Adding element to linked list

  c++, function, linked-list, pointers, struct

Is this a good method for adding new elements in a linked list?

struct node
{
  int info;
  node *next;
};

void addElement(node *&first , int value)
{
  node *q = new node;
  q->info = value;
  q->next = NULL;
  if (first == NULL)
      first = q;
  else
   {
    node *t = new node;
    t = first;
    while (t->next != NULL)
       t=t->next;

   t->next = q;
    }
      
 }

By the way , what does "new" do here?All i know is that new is used for arrays so you can decide their size in the console.Thanks!

Source: Windows Questions C++

LEAVE A COMMENT