position: fixed; top: auto !important; margin-left: 112px;

Linked List Insertion At Front

Linked List insertion can be done using three ways,

1) Insertion at front of given node.

2) Insertion after a given node.

3) Insertion at the end of the node.

In this tutorial, we consider insertion at front of given node. To understand this look at the following figure,

1) Before Insertion at front of the node.

1) The original Linked list contains a head pointer pointing to first node.

2) First Node points to the second Node.

3) The second Node points to NULL.

2) After Insertion at front of node.

Now if we want to insert a node at front of the linked list, it should be like,

1) New node pointing to the first Node.

2) Head pointer pointing to the new node .

3) First Node points to the second Node .

4) The second Node points to NULL.

Playing with the linked list is the very easy task. Just you should know which node will points to which node. This was the beginning of linked list insertion.

Function to insert a node at beginning of Linked List :

void insertAtFront (struct node ** headReference, int dataValue)
   /* 1. allocate node */
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
  /* 2. put data into new node  */
    new_node->data  = dataValue;
  /* 3. Next of New Node is where the head is pointing right now. See in the figure. */
    new_node->next = (*headReference);
   /* 4. Point head to new node finally . */
    (*headReference)    = new_node;

Let's explore this function step by step. To insert a node at the front side of the linked list, you have to create one new node. Right? You have to put some data or value inside that new node. In the third step, next of new node is pointing to head pointer's next node. Just keep diagram in mind and understand the terminology behind the insertion of nodes in Linked list. Then we updated the next pointer of head node to the new node in the fourth step.

Solve the Quiz of Article

1) While implementing the bubble sort algorithm, what do you think algorithm needs n - 1 comparison in Pass 1?

2) What do you think best case performance of Bubble Sort Algorithms is O(n2)?

Previous Next Article


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Copyright 2018. All rights reserved.