Linked List Insertion

Linked List Insertion

In my previous article, I have discussed the basic concept of the linked list and discuss its advantage and disadvantage. In this article, I will discuss how to insert a new node in an existing linked list.

Previously I have described an example where I have created a linked list of four nodes. This is the very good example to create a linked list, but this example is not the generic one.

So here I will create a generic function to create a new node and insert this node in the existing linked list as per the user’s requirement.



There are three ways to insert the new node in an existing linked list.

  • In the beginning of the linked list.
  • After a node in linked list
  • In the end of the linked list.

Insert a node at the beginning of the linked list

Here I am describing few steps to insert a new node at the beginning of the linked list.

  • Allocate the memory in heap for the new node using the calling of malloc().
  • If get the memory successfully then stored the data in the data field as per the requirement.
  • For the linking, assign the address of the first node to the next pointer of the created new node.
  • At the last assign the address of created new to the head pointer.

In InsertNodeAtBeginning() pass the reference of head and data which you want to store in the created node. Every time when you call InsertNodeAtBeginning() it adds the new node at the beginning of the linked list.




Insert a new node after a node

In linked list data are not stored in the contiguous format, so every time we have to start data tracing from the head pointer.

In linked list we can insert a new node after a node, here I am describing few steps to insert a new node at any specified place.

  • Take a node pointer and assigned the address of the first node which is stored by the head pointer.
  • Get the address of the specified node where you want to insert a new node in linked list.
  • If the pTmpNode is not NULL for the specified location then allocate the memory in heap for the new node using the calling of malloc().
  • If get the memory successfully then stored the data in the data field as per the requirement.
  • For the linking, assign the address of the node which is next to the tmp node to the newly created node.
  • Assign the address of the new node to the tmp node.

 




Insert a node at the end of the linked list

In linked list, we can easily append the newly created node. There few steps to append the newly created node to the linked list.

  • Create a node pointer and assigned the address of the first node which is stored by the head pointer.
  • Increment the address of temporary node pointer till not get the address of the last node.
  • Allocate the memory in the heap for the new node.
  • If get the memory successfully then stored the data in the data field as per the requirement.
  • For the linking, assign the address of the newly created node to the tmp node.
  • In the last assigned the NULL to the newly created node as mark the end of the linked list.

 

Note: It is very important to free the allocated memory. If you forgot to do the memory free then might be you get the memory leak issues and your application can be crashed.

So here, I am creating a function to free the all allocated memory.





I am creating a sample program to describe all the function which are defined above.

OutPut:

  • Create a linked-list using two nodes.

Display the created linked-list

  • Insert the node at beginning.

Display the inserted node.

  • Insert the node at the end.

Display the created linked-list

  • Insert the node after a node.

 

Display the created linked-list





 

Leave a Reply