0% found this document useful (0 votes)
1 views

Algorithm to insert a node in circular linked list

The document outlines the process of inserting a new node into a circular linked list at the end, beginning, and after a specific node. It provides detailed steps and algorithms for each insertion method, including memory allocation, traversal, and updating node pointers. The document emphasizes the importance of handling empty lists and ensuring proper linkage between nodes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Algorithm to insert a node in circular linked list

The document outlines the process of inserting a new node into a circular linked list at the end, beginning, and after a specific node. It provides detailed steps and algorithms for each insertion method, including memory allocation, traversal, and updating node pointers. The document emphasizes the importance of handling empty lists and ensuring proper linkage between nodes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Inserting a new element into a circular linked list at the end is quite simple.

There are the


following steps that need to be followed in order to insert a new node in the list at the end.

1. Allocate the space for the new node and store data into the data part of the node and store
NULL at the address part of the node.

NewNode = (struct node *) malloc(sizeof(struct node *));

NewNode → data = item

NewNode->next=NULL

2. We need to declare a temporary pointer temp in order to traverse through the list. Temp is
made to point to the first node of the list.
temp = ptr
3. Then, traverse through the entire linked list
while (temp→ next != ptr)
temp = temp → next;
4. At the end of the loop, the temp will be pointing to the last node of the list. We need to make
the next part of the temp node (which is currently the last node of the list) point to the new
node (ptr).
temp = ptr;
while (temp -> next != ptr)
{
temp = temp -> next;
}
temp->next = NewNode;
NewNode->next=ptr;

Algorithm:
Step 1: IF PTR= NULL Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL
Step 5: SET NEW_NODE - > NEXT = NULL
 Step 6: SET PTR = HEAD
 Step 7: Repeat Step 8 while PTR - > NEXT != PTR
 Step 8: SET PTR = PTR - > NEXT
 [END OF LOOP]
 Step 9: SET PTR - > NEXT = NEW_NODE
 Step 10: SET NEW_NODE->NEXT=PTR
 Step 10: EXIT

Inserting At Beginning of the list

Inserting a new element into a circular linked list at the beginning is quite simple. We just need
to make a few adjustments in the node links. There are the following steps that need to be
followed in order to insert a new node in the list at the beginning.

1. Allocate the space for the new node and store data into the data part of the node and store
NULL at the address part of the node.

NewNode = (struct node *) malloc(sizeof(struct node *));

NewNode → data = item

NewNode->next=NULL
2. We need to declare a temporary pointer temp in order to traverse through the list. Temp is
made to point to the first node of the list.
temp = ptr
3. Then, traverse through the entire linked list
while (temp→ next != ptr)
temp = temp → next;
4. At the end of the loop, the temp will be pointing to the last node of the list. We need to make
the next part of the temp node (which is currently the last node of the list) point to the new
node (ptr).
temp = ptr;
while (temp -> next != ptr)
{
temp = temp -> next;
}
NewNode->next=ptr;
ptr=NewNode;
temp->next=NewNode;

Algorithm to insert a new node at the beginning


Step 1: IF PTR= NULL Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL
Step 5: SET NEW_NODE - > NEXT = NULL
Step 6: SET PTR = HEAD
Step 7: Repeat Step 8 while PTR - > NEXT != PTR
Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
Step 9: SET NEW_NODE - > NEXT = PTR
Step 10: SET PTR=NEW_NODE
Step 11: SET PTR->NEXT=NEW_NODE

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the circular linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set head = newNode and newNode → next = head.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location
is the node value after which we want to insert the newNode).
 Step 6 - Every time check whether temp is reached to the last node or not. If it is reached
to last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
 Step 7 - If temp is reached to the exact node after which we want to insert the newNode
then check whether it is last node (temp → next == head).
 Step 8 - If temp is last node then set temp → next = newNode and newNode →
next = head.
 Step 8 - If temp is not last node then set newNode → next = temp → next and temp →
next = newNode.

You might also like