We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.

All programs discussed in this post consider following representations of linked list

C Programming:

[pastacode lang=”c” manual=”%2F%2F%20A%20linked%20list%20node%0Astruct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*next%3B%0A%7D%3B” message=”” highlight=”” provider=”manual”/]

In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list.

[ad type=”banner”]

Add a node at the front: (A 4 steps process)
The new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example if the given Linked List is 10->15->20->25 and we add an item 5 at the front, then the Linked List becomes 5->10->15->20->25. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node (See this)

C Algorithm - Inserting a node in Linked List | Set 2

Following are the 4 steps to add node at the front.

C Programming:

[pastacode lang=”c” manual=”%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%20of%20a%20list%0A%20%20%20and%20an%20int%2C%20%20inserts%20a%20new%20node%20on%20the%20front%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%201.%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%20%0A%20%20%20%20%2F*%202.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%0A%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20node%20as%20head%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%20%0A%20%20%20%20%2F*%204.%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Time complexity of push() is O(1) as it does constant amount of work.

[ad type=”banner”]

Add a node after a given node: (5 steps process)
We are given pointer to a node, and the new node is inserted after the given node.

C Programming - Inserting a node in Linked List | Set 2
C Programming:

[pastacode lang=”c” manual=”%2F*%20Given%20a%20node%20prev_node%2C%20insert%20a%20new%20node%20after%20the%20given%0A%20%20%20prev_node%20*%2F%0Avoid%20insertAfter(struct%20node*%20prev_node%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*1.%20check%20if%20the%20given%20prev_node%20is%20NULL%20*%2F%0A%20%20%20%20if%20(prev_node%20%3D%3D%20NULL)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20printf(%22the%20given%20previous%20node%20cannot%20be%20NULL%22)%3B%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20return%3B%20%20%0A%20%20%20%20%7D%20%20%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%202.%20allocate%20new%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%20%0A%20%20%20%20%2F*%203.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%0A%20%20%20%20%2F*%204.%20Make%20next%20of%20new%20node%20as%20next%20of%20prev_node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20prev_node-%3Enext%3B%20%0A%20%20%0A%20%20%20%20%2F*%205.%20move%20the%20next%20of%20prev_node%20as%20new_node%20*%2F%0A%20%20%20%20prev_node-%3Enext%20%3D%20new_node%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Time complexity of insertAfter() is O(1) as it does constant amount of work.

[ad type=”banner”]

Add a node at the end: (6 steps process)
The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30.
Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.

C Programming - Inserting a node in Linked List | Set 2

Following are the 6 steps to add node at the end

C Programming:

[pastacode lang=”c” manual=”%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20%20of%20a%20list%20and%20an%20int%2C%20appends%20a%20new%20node%20at%20the%20end%20%20*%2F%0Avoid%20append(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%201.%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20struct%20node%20*last%20%3D%20*head_ref%3B%20%20%2F*%20used%20in%20step%205*%2F%0A%20%20%0A%20%20%20%20%2F*%202.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%203.%20This%20new%20node%20is%20going%20to%20be%20the%20last%20node%2C%20so%20make%20next%20%0A%20%20%20%20%20%20%20%20%20%20of%20it%20as%20NULL*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%204.%20If%20the%20Linked%20List%20is%20empty%2C%20then%20make%20the%20new%20node%20as%20head%20*%2F%0A%20%20%20%20if%20(*head_ref%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20*head_ref%20%3D%20new_node%3B%0A%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%2F*%205.%20Else%20traverse%20till%20the%20last%20node%20*%2F%0A%20%20%20%20while%20(last-%3Enext%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20last%20%3D%20last-%3Enext%3B%0A%20%20%0A%20%20%20%20%2F*%206.%20Change%20the%20next%20of%20last%20node%20*%2F%0A%20%20%20%20last-%3Enext%20%3D%20new_node%3B%0A%20%20%20%20return%3B%20%20%20%20%0A%7D” message=”” highlight=”” provider=”manual”/]

Time complexity of append is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work.
This method can also be optimized to work in O(1) by keeping an extra pointer to tail of linked list

[ad type=”banner”]

Following is a complete program that uses all of the above methods to create a linked list.

C Programming:

[pastacode lang=”c” manual=”%2F%2F%20A%20complete%20working%20C%20program%20to%20demonstrate%20all%20insertion%20methods%0A%2F%2F%20on%20Linked%20List%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20A%20linked%20list%20node%0Astruct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%20of%20a%20list%20and%20%0A%20%20%20an%20int%2C%20inserts%20a%20new%20node%20on%20the%20front%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%201.%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%202.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20node%20as%20head%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%204.%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Given%20a%20node%20prev_node%2C%20insert%20a%20new%20node%20after%20the%20given%20%0A%20%20%20prev_node%20*%2F%0Avoid%20insertAfter(struct%20node*%20prev_node%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*1.%20check%20if%20the%20given%20prev_node%20is%20NULL%20*%2F%0A%20%20%20%20if%20(prev_node%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20printf(%22the%20given%20previous%20node%20cannot%20be%20NULL%22)%3B%0A%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%202.%20allocate%20new%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%203.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%204.%20Make%20next%20of%20new%20node%20as%20next%20of%20prev_node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20prev_node-%3Enext%3B%0A%20%0A%20%20%20%20%2F*%205.%20move%20the%20next%20of%20prev_node%20as%20new_node%20*%2F%0A%20%20%20%20prev_node-%3Enext%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20%20of%20a%20list%20and%20an%20int%2C%20appends%20a%20new%20node%20at%20the%20end%20%20*%2F%0Avoid%20append(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%201.%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20struct%20node%20*last%20%3D%20*head_ref%3B%20%20%2F*%20used%20in%20step%205*%2F%0A%20%0A%20%20%20%20%2F*%202.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%203.%20This%20new%20node%20is%20going%20to%20be%20the%20last%20node%2C%20so%20make%20next%20of%0A%20%20%20%20%20%20%20%20%20%20it%20as%20NULL*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%204.%20If%20the%20Linked%20List%20is%20empty%2C%20then%20make%20the%20new%20node%20as%20head%20*%2F%0A%20%20%20%20if%20(*head_ref%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20*head_ref%20%3D%20new_node%3B%0A%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%205.%20Else%20traverse%20till%20the%20last%20node%20*%2F%0A%20%20%20%20while%20(last-%3Enext%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20last%20%3D%20last-%3Enext%3B%0A%20%0A%20%20%20%20%2F*%206.%20Change%20the%20next%20of%20last%20node%20*%2F%0A%20%20%20%20last-%3Enext%20%3D%20new_node%3B%0A%20%20%20%20return%3B%0A%7D%0A%20%0A%2F%2F%20This%20function%20prints%20contents%20of%20linked%20list%20starting%20from%20head%0Avoid%20printList(struct%20node%20*node)%0A%7B%0A%20%20while%20(node%20!%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20%20printf(%22%20%25d%20%22%2C%20node-%3Edata)%3B%0A%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%0A%7B%0A%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%0A%20%20%2F%2F%20Insert%206.%20%20So%20linked%20list%20becomes%206-%3ENULL%0A%20%20append(%26head%2C%206)%3B%0A%20%0A%20%20%2F%2F%20Insert%207%20at%20the%20beginning.%20So%20linked%20list%20becomes%207-%3E6-%3ENULL%0A%20%20push(%26head%2C%207)%3B%0A%20%0A%20%20%2F%2F%20Insert%201%20at%20the%20beginning.%20So%20linked%20list%20becomes%201-%3E7-%3E6-%3ENULL%0A%20%20push(%26head%2C%201)%3B%0A%20%0A%20%20%2F%2F%20Insert%204%20at%20the%20end.%20So%20linked%20list%20becomes%201-%3E7-%3E6-%3E4-%3ENULL%0A%20%20append(%26head%2C%204)%3B%0A%20%0A%20%20%2F%2F%20Insert%208%2C%20after%207.%20So%20linked%20list%20becomes%201-%3E7-%3E8-%3E6-%3E4-%3ENULL%0A%20%20insertAfter(head-%3Enext%2C%208)%3B%0A%20%0A%20%20printf(%22%5Cn%20Created%20Linked%20list%20is%3A%20%22)%3B%0A%20%20printList(head)%3B%0A%20%0A%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

 Created Linked list is:  1  7  8  6  4