Here you will get the program for circular connected list in C.
What is Circular Linked List?
A circular connected rundown is a connected rundown where the last hub focuses on the head or front hub making
the information structure to resemble a circle. A circularly connected rundown hub can be executed utilizing
separately connected or doubly connected rundown.
The underneath portrayal shows how a circular connected rundown resembles. In contrast to
the directly connected rundown, the last hub is indicated the head utilizing a back pointer.
Program for Circular Linked List in C
We essentially have 3 hubs head, back and temp to execute a circular connected rundown.
The back focuses on the last hub of the rundown, head focuses on the first hub. This keeps up a track on
where is the front and back hubs of the circle. Give us a chance to see the fundamental tasks, for example,
creation, cancellation and showing off the components of the circular connected rundown.
Formation of Node
This is a lot of like that of making a hub in an independently
connected rundown. It includes making another hub and relegating information and indicating the
present hub the leader of the circular connected rundown. The code is as demonstrated as follows.
void create()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the node value : ");
scanf("%d",&newnode->info);
newnode->next=NULL;
if(rear==NULL)
front=rear=newnode;
else
{
rear->next=newnode;
rear=newnode;
}
rear->next=front;
}
Cancellation of Node
We customarily erase the front hub from the rundown in this program. To erase a hub,
we have to check if the rundown is vacant. On the off chance that it isn’t unfilled, at that
point indicate the back hub the front->next and back >next to the front. This evacuates the principal hub.
void del()
{
temp=front;
if(front==NULL)
printf("\nUnderflow :");
else
{
if(front==rear)
{
printf("\n%d",front->info);
front=rear=NULL;
}
else
{
printf("\n%d",front->info);
front=front->next;
rear->next=front;
}
temp->next=NULL;
free(temp);
}
}
Navigating Circular Linked List
Navigating the circular rundown begins from the front hub
and iteratively proceeds until the back hub. The accompanying capacity is utilized for this reason.
Program
void display()
{
temp=front;
if(front==NULL)
printf("\nEmpty");
else
{
printf("\n");
for(;temp!=rear;temp=temp->next)
printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);
printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);
}
}
Complete Program
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int info;
struct Node *next;
}node;
node *front=NULL,*rear=NULL,*temp;
void create();
void del();
void display();
int main()
{
int chc;
do
{
printf("\nMenu\n\t 1 to create the element : ");
printf("\n\t 2 to delete the element : ");
printf("\n\t 3 to display the queue : ");
printf("\n\t 4 to exit from main : ");
printf("\nEnter your choice : ");
scanf("%d",&chc);
switch(chc)
{
case 1:
create();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
return 1;
default:
printf("\nInvalid choice :");
}
}while(1);
return 0;
}
void create()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the node value : ");
scanf("%d",&newnode->info);
newnode->next=NULL;
if(rear==NULL)
front=rear=newnode;
else
{
rear->next=newnode;
rear=newnode;
}
rear->next=front;
}
void del()
{
temp=front;
if(front==NULL)
printf("\nUnderflow :");
else
{
if(front==rear)
{
printf("\n%d",front->info);
front=rear=NULL;
}
else
{
printf("\n%d",front->info);
front=front->next;
rear->next=front;
}
temp->next=NULL;
free(temp);
}
}
void display()
{
temp=front;
if(front==NULL)
printf("\nEmpty");
else
{
printf("\n");
for(;temp!=rear;temp=temp->next)
printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);
printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);
}
}
Output
The above program brings about the accompanying output. You can see that the last hub focuses
on the principal hub address that is the primary subject of circular connected rundown in C.