What is Circular Linked List?
In single linked list, every node points to its next node in the sequence and the last node points NULL. But in circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list.
Example
Operations
In a circular linked list, we perform the following operations...
- Insertion
- Deletion
- Display
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows...
- Inserting At Beginning of the list
- Inserting At End of the list
- Inserting At Specific location in the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the circular linked list...
- Step 1 - Create a node ptr with given value.
- Step 2 - Check whether list is Empty (head == NULL)
- Step 3 - If it is Empty then, set head = ptr and ptr -> 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 last node (until 'temp -> next == head').
- Step 6 - Set 'ptr -> next =head', 'head = ptr' and 'temp -> next = head'.
Inserting At End of the list
We can use the following steps to insert a new node at end of the circular linked list...
- Step 1 - Create a node ptr with given value.
- Step 2 - Check whether list is Empty (head == NULL).
- Step 3 - If it is Empty then, set head = ptr and ptr -> 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 last node in the list (until temp → next == head).
- Step 6 - Set temp -> next = ptr and ptr -> next = head.
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 node ptr with given value.
- Step 2 - Check whether list is Empty (head == NULL)
- Step 3 - If it is Empty then, set head = ptr and ptr -> 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 ptr-> 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 = ptr and ptr -> next = head.
- Step 8 - If temp is not last node then set ptr -> next = temp -> next and temp -> next = ptr.
Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...
- Deleting from Beginning of the list
- Deleting from End of the list
- Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the circular linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
- Step 3 - If it is Not Empty then, define Node pointers 'ptr' and initialize 'ptr' with head.
- Step 4 - Check whether list is having only one node (ptr -> next == head)
- Step 5 - If it is TRUE then set head = NULL and delete ptr (Setting Empty list conditions)
- Step 6 - If it is FALSE move the ptr until it reaches to the last node. (until ptr -> next == head )
- Step 7 - Then set head = ptr -> next, ptr -> next = head -> next and delete head.
Deleting from End of the list
We can use the following steps to delete a node from end of the circular linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
- Step 3 - If it is Not Empty then, define two Node pointers 'ptr' and 'preptr' and initialize ptr' with head.
- Step 4 - Check whether list has only one Node (ptr -> next == head)
- Step 5 - If it is TRUE. Then, set head = NULL and delete ptr. And terminate from the function. (Setting Empty list condition)
- Step 6 - If it is FALSE. Then, set 'preptr = ptr ' and move ptr to its next node. Repeat the same until ptr reaches to the last node in the list. (until ptr -> next == head)
- Step 7 - Set preptr -> next = ptr -> next and delete ptr.
Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the circular linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
- Step 3 - If it is Not Empty then, define two Node pointers 'ptr1' and 'ptr2' and initialize 'ptr1' with head.
- Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And every time set 'temp2 = temp1' before moving the 'ptr1' to its next node.
- Step 5 - If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!!'. And terminate the function.
- Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node (ptr1 -> next == head)
- Step 7 - If list has only one node and that is the node to be deleted then set head = NULL and delete ptr1 (free(ptr1)).
- Step 8 - If list contains multiple nodes then check whether temp1 is the first node in the list (ptr1 == head).
- Step 9 - If ptr1 is the first node then set ptr2 = head and keep moving ptr2 to its next node until ptr2 reaches to the last node. Then set head = head -> next, ptr2 -> next = head and delete ptr1.
- Step 10 - If ptr1 is not first node then check whether it is last node in the list (ptr1 -> next == head).
- Step 11- If ptr1 is last node then set ptr2 -> next = head and delete ptr1 (free(ptr1)).
- Step 12 - If ptr1 is not first node and not last node then set ptr2 -> next = ptr1 -> next and delete ptr1 (free(ptr1)).
Displaying a circular Linked List
We can use the following steps to display the elements of a circular linked list...
- Step 1 - Check whether list is Empty (head == NULL)
- Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
- Step 3 - If it is Not Empty then, define a Node pointer 'ptr' and initialize with head.
- Step 4 - Keep displaying ptr -> data with an arrow (--->) until ptr reaches to the last node
- Step 5 - Finally display ptr -> data with arrow pointing to head -> data.
Implementation of Circular Linked List using C Programming
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void main ()
{
int choice =0;
while(choice != 8)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert random\n4.Delete from Begin\n5.Delete from last\n6.Delete from random\n7.Show\n8.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("\nnode inserted\n");
}
}
void randominsert()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter the value to be inserted: ");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
printf("Enter the location after which you want to insert: ");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp -> next;
if(temp -> next == head)
{
printf("Given node is not found in the list!!!");
return;
}
}
ptr -> next = temp -> next;
temp -> next = ptr;
printf("\nInsertion success!!!");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");
}
}
void random_delete()
{
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct node *ptr1 = head, *ptr2;
int delValue;
printf("Enter the item which you want to delete: ");
scanf("%d",&delValue);
while(ptr1 -> data != delValue)
{
if(ptr1 -> next == head)
{
printf("\nGiven node is not found in the list!!!");
}
else
{
ptr2 = ptr1;
ptr1 = ptr1 -> next;
}
}
if(ptr1 -> next == head){
head = NULL;
free(ptr1);
}
else{
if(ptr1 == head)
{
ptr2 = head;
while(ptr2 -> next != head)
ptr2 = ptr2 -> next;
head = head -> next;
ptr2 -> next = head;
free(ptr1);
}
else
{
if(ptr1 -> next == head)
{
ptr2 -> next = head;
}
else
{
ptr2 -> next = ptr1 -> next;
}
free(ptr1);
}
}
printf("\nDeletion success!!!");
}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
while(ptr -> next != head)
{
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void main ()
{
int choice =0;
while(choice != 8)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert random\n4.Delete from Begin\n5.Delete from last\n6.Delete from random\n7.Show\n8.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("\nnode inserted\n");
}
}
void randominsert()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter the value to be inserted: ");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
printf("Enter the location after which you want to insert: ");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp -> next;
if(temp -> next == head)
{
printf("Given node is not found in the list!!!");
return;
}
}
ptr -> next = temp -> next;
temp -> next = ptr;
printf("\nInsertion success!!!");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");
}
}
void random_delete()
{
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct node *ptr1 = head, *ptr2;
int delValue;
printf("Enter the item which you want to delete: ");
scanf("%d",&delValue);
while(ptr1 -> data != delValue)
{
if(ptr1 -> next == head)
{
printf("\nGiven node is not found in the list!!!");
}
else
{
ptr2 = ptr1;
ptr1 = ptr1 -> next;
}
}
if(ptr1 -> next == head){
head = NULL;
free(ptr1);
}
else{
if(ptr1 == head)
{
ptr2 = head;
while(ptr2 -> next != head)
ptr2 = ptr2 -> next;
head = head -> next;
ptr2 -> next = head;
free(ptr1);
}
else
{
if(ptr1 -> next == head)
{
ptr2 -> next = head;
}
else
{
ptr2 -> next = ptr1 -> next;
}
free(ptr1);
}
}
printf("\nDeletion success!!!");
}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
while(ptr -> next != head)
{
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}
Output:
No comments:
Post a Comment