Today we will discuss how to delete a node at kth position in circular linked list. But if you are unfamiliar with the concepts of circular linked list then please refer to this link . Suppose you have a circular linked list 1->2->3->4 and 4 pointing to 1. Let k=2 then we have to delete the second node. After deletion the list should be 1->3->4 and 4 pointing to 1. Now, let's discuss the procedure:
Procedure
1. First we will make a circular linked list.
2. Then we will traverse the list until we reach the node which is just previous to the kth node.
3. Now we will update the pointer of this previous node with the address of the node next to kth node.
4. If the value of 'k' is 1 then we have to delete the first node. While doing so we will traverse till last node and then we will update the pointer of this node with the address of the node next to 1st node. But we will also have to update the head pointer because if the first node is deleted then second node will now be the first node.
Code
Explanation of code
1. First we will write a simple code of circular linked list which will have 3 functions:
- insert() - which will insert elements in the circular linked list.
- assignhead() - to make a link between last and first node.
- disp() - which will display the circular linked list.
- assignhead() - to make a link between last and first node.
- disp() - which will display the circular linked list.
2. Insert some values in the circular linked list.
3. Now we have created a function named Delete() to delete a node at kth the circular linked list. It has a parameter 'k' which tells us about the position of the node to be deleted.
4. First we have checked if 'k' is not equals to 1 because if we have divided out problem into 2 parts. If the node which is to be deleted is the first node or any other node.
5. If 'k' is not equal to 1 then we have created a pointer to node variable 'temp' which is pointing to head. We have also created a variable 'c' which is a counter to traverse the list till the node which is just previous to kth node.
if(k!=1)
{
struct node* temp=head;
int c=1;
{
struct node* temp=head;
int c=1;
---
---
}
6. Then we have created a while loop which will iterate the list till we reach the node which is just previous to kth node.
while(c<k-1)
{
temp=temp->next;
c++;
}
{
temp=temp->next;
c++;
}
7. Then we have updated the next pointer of temp with the next pointer of the kth node because the node just previous to kth node shall point to the node which is next to the kth node. (kth node is not physically deleted from the list, we have just changed the pointers so that it shall not come in the output screen ).
temp->next=temp->next->next;
8. Now we have reached the else part which will be executed if k is equal to 1 or if we want to delete the first node.
9. We have again created a pointer to node variable 'temp' which is pointing to head.
else
{
struct node* temp=head;
{
struct node* temp=head;
---
---
}
10. Then we have created a while loop which will iterate the list till we reach the last node whose next pointer is pointing to the first node again.
while(temp->next!=head)
{
temp=temp->next;
}
{
temp=temp->next;
}
11. Then we have updated the head and assigned it the address of the node next to first node.
head=temp->next->next;
12. Then we have updated the next pointer of temp with the address of the node next to kth node . (kth node is not physically deleted from the list, we have just changed the pointers so that it shall not come in the output screen ).
temp->next=temp->next->next;