Circular Linked list Implementation code is given below:
//Code for Implementation of Circular Linked List
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
} *head = NULL;
struct node* tail;
int length()
{
int length = 0;
struct node *t;
t=head;
while(t->link != head)
{
length++;
t = t ->link;
}
length++;
return length;
}
void insertbegin(int item)
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = item;
newnode->link = newnode;
if (head == NULL)
{
head= tail = newnode;
}
else
{
newnode->link = head;
head = newnode;
tail->link=head;
}
}
void insertend(int item)
{
struct node *newnode, *t;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = item;
newnode->link = newnode;
t = head;
if (head == NULL)
{
head = tail= newnode;
}
else
{
tail->link=newnode;
newnode->link=head;
tail=newnode;
}
}
void insertpos(int item, int pos)
{
struct node *newnode, *t;
int i;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = item;
newnode->link = newnode;
t = head;
int l=length();
if (pos>l+1)
{
printf("\nInvalid position");
}
else if(pos == 1)
{
insertbegin(item);
}
else if(pos == l+1)
{
insertend(item);
}
else{
for(int i=1;i<pos-1;i++)
{
t=t->link;
}
newnode->link=t->link;
t->link=newnode;
}
}
void deletefront()
{
struct node *t;
if (head == NULL)
{
printf("empty");
}
else if(head->link==head)
{
printf("\ndeleted data is %d ", head->data);
head = tail = NULL;
}
else
{
t = head;
printf("\ndeleted data is %d ", t->data);
head = head->link;
tail->link=head;
free(t);
}
}
void deleteend()
{
struct node *t1, *t2;
t2 = head;
if (head == NULL)
{
printf(" list is empty");
}
else if(head->link==head)
{
printf("\ndeleted data is %d ", head->data);
head = tail = NULL;
}
else
{
while (t2->link != tail->link)
{
t1=t2;
t2=t2->link;
}
if(t2==head)
{
head = NULL;
}
else{
t1->link=t2->link;
tail=t1;
}
printf("\n deleted element is %d ", t2->data);
free(t2);
}
}
void deletepos(int pos)
{
int l=length();
struct node *t1, *t2;
t2 = head;
if (head == NULL)
{
printf("empty");
}
else
{
if(pos > l)
{
printf("Invalid position");
}
else if(pos == 1)
{
deletefront();
}
else if(pos == l)
{
deleteend();
}
else{
for(int i =1;i<pos;i++)
{
t1=t2;
t2=t2->link;
}
t1->link=t2->link;
printf("\ndeleted element is %d ", t2->data);
free(t2);
}
}
}
void display()
{
struct node *t = head;
if(head == NULL)
{
printf("LIst is empty");
}
else{
while (t->link != head)
{
printf("_%d_", t->data);
t = t->link;
}
printf("_%d_", t->data);
}
}
void main()
{
int choice ,c,data,x;
do{
printf("press 1 to insert in begning\n");
printf("press 2 to insert in end\n");
printf("press 3 to insert in any positions\n");
printf("press 4 to delete first element\n");
printf("press 5 to delete end element\n");
printf("press 6 to to delete in any position\n");
printf("press 7 to display\n");
printf("press 8 for the length of current list\n");
printf("press 0 to Exit\n");
fflush(stdin);
scanf(" %d",&choice);
switch(choice)
{
case 1 :
printf(" Enter the data to insert :");
scanf(" %d",&data);
insertbegin(data);
break;
case 2 :
printf(" Enter the data to insert :");
scanf(" %d",&data);
insertend(data);
break;
case 3 :
printf(" Enter the data to insert :");
scanf(" %d",&data);
printf(" Enter the position to insert :");
scanf(" %d",&x);
insertpos(data,x);
break;
case 4 :
deletefront();
break;
case 5 :
deleteend();
break;
case 6 :
printf(" Enter the position to delete :");
scanf(" %d",&x);
deletepos(x);
break;
case 7 :
display();
break;
case 8 :
printf("\n the length of current list :%d",length());
break;
case 0:
exit(1);
break;
default:
printf("\nEnter the correct value");
}
printf("\nEnter 1 to continue :");
fflush(stdin);
scanf(" %d",&c);
}while(c==1);
}
Output
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
1
Enter the data to insert :55
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_55__22__11__33__66_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
5
deleted element is 66
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_55__22__11__33_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
6
Enter the position to delete :2
deleted element is 22
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
7
_55__11__33_
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
8
the length of current list :3
Enter 1 to continue :1
press 1 to insert in begning
press 2 to insert in end
press 3 to insert in any positions
press 4 to delete first element
press 5 to delete end element
press 6 to to delete in any position
press 7 to display
press 8 for the length of current list
press 0 to Exit
0