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 a 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

1.  At the beginning of the linked list.
2. After a node in the linked list
3. At the end of the linked list.

 

Insert a node at the beginning of the linked list

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

1. Allocate the memory in heap for the new node using the calling of malloc().

 

2. After getting the memory successfully stored the data in the data field as per the requirement.

 

3. For the linking, assign the address of the first node to the next pointer of the created new node.

 

4. At the last assign the address of created new to the head pointer.

 

 

See the below sample code when the user calls the function ( InsertNodeAtBeginning() ) then it adds the new node at the beginning of the linked list.

 

If you want to learn more about the data structure, here 10 Free days online video course for you.

Click Here to Get the Course

 

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. Below I am describing the steps to how you can insert a new node after a given node.

1. Take a node pointer and assigned the address of the first node which is stored by the head pointer.

 

2. Get the address of the specified node where you want to insert a new node in the linked list.

 

3. 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().

 

4. After getting the memory successfully stored the data in the data field as per the requirement.

 

5. For the linking, assign the address of the node which is next to the tmp node to the newly created node.

 

6. Assign the address of the new node to the tmp node.

 

 




Insert a node at the end of the linked list

In the linked list, we can easily append the newly created node. Below I have mention steps to append the newly created node in the linked list.

1. Create a node pointer and assigned the address of the first node which is stored by the head pointer.

 

2. Increment the address of temporary node pointer till not get the address of the last node.

 

3. Allocate the memory in the heap for the new node.

 

4. After getting the memory successfully stored the data in the data field as per the requirement.

 

5. For the linking, assign the address of the newly created node to the tmp node.

 

6. In the last assigned the NULL to the newly created node as mark the end of the linked list.

 

Note: Don’t forget to free the allocated memory.




Free the Allocated memory

 

 

In below program, I am creating a linked list where I am adding the node at the beginning, end and at any position of the linked list.

OutPut:

1. Create a linked-list using two nodes.

 

 

 

 

2. Insert the node at the beginning.

 

 

Display the inserted node

 

 

3. Insert the node at the end

 

 

Display the created linked-list

 

 

4. Insert the node after a node.

 

 

Display the created linked-list

 



One comment

Leave a Reply