A circular linked list is very similar to a linear linked list except that it no longer uses null to terminate the list. The tail now points to the head, erasing the need for a null reference. A circular linked list saves the requirement of a head node, but the algorithms of iterating a circular linked list become slightly more complicated.
Language : C
[sourcecode language='c']
//Circular Linked List
#include
#include
#include
void insertion();
void display();
void del();
int ctr=0;
struct node{
int a;
struct node *next;
}*list,*temp,*save,*pred;
void main()
{ int a;
char ch;
list=(struct node *)malloc(sizeof(struct node));
list=NULL;
do{
printf(“\n1.Insertion\n2.Display\n3.Delete: “);
scanf(“%d”, &a);
switch(a)
{
case 1: insertion();
break;
case 2:display();
break;
case 3: del();
break;
default :
printf(“\nWrong Choice!!!”);
}
printf(“\nMENU ?”);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’y’ || ch==’Y');
}
void insertion()
{
int x,a,pos,ctr,af;
char ch;
do {
temp=(struct node *)malloc(sizeof(struct node));
printf(“\nEnter the element : “);
scanf(“%d”, &x);
::ctr++;
if(list==NULL) //if EMPTY Performs insert at FRONT
{
temp->a=x;
temp->next=list;
list=temp;
break;
}
printf(“Enter the position : “);
scanf(“%d”, &pos);
save=list;
ctr=0;
pred=NULL;
while(ctr
if(pos>::ctr)
{
printf(“Wrong position!”);
return;
}
pred=list;
list=list->next;
ctr++;
}
printf(“Insert 1.After or 2.Before the element %d :”, list->a);
scanf(“%d”, &af);
if(af==1)
{
temp->a=x;
temp->next=list->next;
list->next=temp;
}
else
{ list=pred;
temp->a=x;
temp->next=list->next;
list->next=temp;
}
list=save;
printf(“Continue Insertion (Y/N)?”);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’Y’ || ch==’y');
display();
}
void display()
{ int i=0;
if(list==NULL)
{
printf(“\nThe list is empty!”);
return;
}
save=list;
while(i
printf("\t%d", list->a);
list=list->next;
}
list=save;
}
void del()
{ int ctr,pos;
int i=0;
char ch;
if(list==NULL)
{
printf(“\nUnderflow!!!”);
return;
}
do{
temp=NULL;
printf(“Enter the position : “);
scanf(“%d”, &pos);
save=list;
ctr=0;
while(ctr
if(list==NULL)
{
printf("Wrong position!");
return;
}
temp=list;
list=list->next;
ctr++;
}
printf(“\nThe deleted element is : %d”, list->a);
temp->next=list->next;
list->next=NULL;
free(list);
list=save;
::ctr–;
printf(“\nContinue Deletion (Y/N)? : “);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’y’ || ch==’Y');
}
[/sourcecode]






