-3

In linked list implementation, the insertion of a new node to the linked list is usually written like this:

void push(struct node** head_ref, int new_data)

/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));

/* 2. put in the data  */
new_node->data  = new_data;

/* 3. Make next of new node as head */
new_node->next = (*head_ref);

/* 4. move the head to point to the new node */
(*head_ref)    = new_node;

(I took this code from http://quiz.geeksforgeeks.org/linked-list-set-2-inserting-a-node/)

and the node struct is:

struct node {

int data;

struct node *next;

};

What I don't understand is the 3. and 4. of the insertion part. So you make the next pointer of new_node pointed to the head, and then the head points to the new_node? So that means the next pointer points to new_node?

It seems like a stupid question but I'm having trouble understanding it, so I hope someone can explain it to me. Thank you.

Khang Bùi
  • 9
  • 1
  • 2

5 Answers5

1

Well basically in a linked list all nodes are connected to each other. It depends upon u that where do u insert a new node either at end or start. Each time we insert a new node we will check the head pointer.

   if(head == NULL)  //it means that the node is empty
   {
      head = newNode;  //so we will assign the new node to the head
   }
    else
   {
      node* temp = head;       //creating a temp pointer that will go 
                               // to the end of the linked list

      while(temp -> next != NULL) { temp = temp->next; }
      temp = newNode;          //This will add the new node to the end
      newNode->next = NULL;enter code here 
    }
0

If I understood correctly this is your scenario?

http://www.kkhsou.in/main/EVidya2/computer_science/comscience/313.gif

Your list is just a linked list with next pointer until list's last item that has null as pointer

Step 3 makes your new node to point to 2nd item that was at beginning of the list before this operation

Step 4 makes the list head to point to the new node

Hope this helps

Leolian
  • 181
  • 1
  • 1
  • 8
0

/* 1. allocate node / struct node new_node = (struct node*) malloc(sizeof(struct node));

/* 2. put in the data */ new_node->data = new_data;

/* 3. Make next of new node as head */ new_node->next = (*head_ref);

/* 4. move the head to point to the new node */ (*head_ref) = new_node;

In Step1 and 2, a new node is created and data is assigned to it.

When you list is empty, your *head_ref would be null. or else if it has any elements, it would be pointing to that Lets take an example

Now 
*head_ref is null
when input is 1
newnode.data=1
newnode.next=null
*headref=newnode 
now your *headref points to the latest node that is added ,this happens with step4

When you insert 2
newnode.data=2
newnode.next=*headref(to the node which is 1)
newnode=*headref
now your list is
1->2(*headref)
now if you add 3 here
it becomes
1->2->3(*headref)

Hope you understand

Geeky
  • 7,420
  • 2
  • 24
  • 50
0

Rather than explaining it to you, I'm going to suggest a technique that will help you work out the answer for yourself.

  1. Get a piece of paper, a pencil and an eraser.

  2. Draw a box on the paper to represent each variable in your algorithm

  3. Draw the initial linked list:

    • Draw a box to represent each existing node in the initial linked list.
    • Divide each box into sub-boxes representing the fields.
    • In each field write either a value, or a dot representing the "from" end of a pointer.
    • For each pointer, draw a line to the thing (e.g. node) that is pointed to, and put an arrowhead on the "to" end. A NULL pointer is a just a dot.
  4. Now execute the algorithm.

    • Each time you allocate a new node, draw a new box.
    • Each time you assign something to a variable, or a field, rub out the current value and write / draw in the new value or the new dot / arrow.

If you do this carefully and systematically, you will be able to visualize exactly what the list insertion algorithm is doing.

This same technique can be used to visualize any list / tree / graph algorithm ... modulo your ability to get it all onto a sheet of paper, and the paper's ability to survive repeated rub-outs.


(This pencil and paper approach is very "old school". As in, this is what we were taught to do when I learned to program in the 1970's. A slightly more modern approach would be to use a whiteboard ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

First of all head pointer is pointing to first node in list. In (1) new node is created. In (2) data is saved to new node. In (3) The new node is pointing where the head is pointing(means to the first node) In (4) now the head is made to point to new node, so therefore the new node is now the first node. thats it.