//fms
//program to implement circular linked list
#include<malloc.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int info;
struct node *link;
}*last;
typedef struct node *nod;
void main()
{
int ch,n,m,i,pos,c,k,a,ch1;
last->link=last;
clrscr();
printf("\nMENU\n1.create\n2.dispaly in sorted order\n3.insert\n 1.front\n 2.end\n 3.any\n4.delete\n 1.front\n 2.end\n 3.any\n5.modify\n6.exit");
do
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element:");
scanf("%d",&m);
create_l(m);
}
break;
case 2:printf("\nElements in sorted order are.....\n");
display();
break;
case 3:do
{
printf("\nEnter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:printf("\nEnter the element");
scanf("%d",&m);
insertf(m);
break;
case 2:printf("\nEnter the element");
scanf("%d",&m);
inserte(m);
break;
case 3:printf("\nEnter the element");
scanf("%d",&m);
printf("\nEnter key:");
scanf("%d",&k);
inserta(k,m);
break;
default:printf("invalid choice");
}
printf("\nDo you need 2 continue 1.yes 2.no");
scanf("%d",&a);
}while(a==1);
break;
case 4:do
{
printf("\nEnter your choice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:delf();
break;
case 2:dele();
break;
case 3:printf("\nEnter the element");
scanf("%d",&m);
dela(m);
break;
}
printf("\nDo you need 2 continue 1.yes 2.no");
scanf("%d",&a);
}while(a==1);
break;
case 5:printf("\nEnter the element");
scanf("%d",&m);
printf("\nEnter key:");
scanf("%d",&k);
modify(k,m);
break;
case 6:exit(0);
}
}while(1);
getch();
}
create_l(int m)
{
nod q,tmp;
tmp=malloc(sizeof(struct node));
tmp->info=m;
if(last->link==last)
{
tmp->link=last;
last->link=tmp;
}
else
{
q=last;
while(q->link!=last)
{
q=q->link;
}
tmp->link=last;
q->link=tmp;
}
disp();
return;
}
inserte(int m)
{
nod q,tmp;
tmp=malloc(sizeof(struct node));
tmp->info=m;
if(last->link==last)
{
tmp->link=last;
last->link=tmp;
}
else
{
q=last;
while(q->link!=last)
{
q=q->link;
}
tmp->link=last;
q->link=tmp;
disp();
}
return;
}
insertf(int m)
{
nod tmp;
tmp=malloc(sizeof(struct node));
tmp->info=m;
tmp->link=last->link;
last->link=tmp;
disp();
return;
}
disp()
{
nod q,c,t;
if(last->link==last)
printf("\nList empty");
q=last->link;
printf("->");
while(q!=last)
{
printf("%d->",q->info);
q=q->link;
}
return;
}
inserta(int key,int data)
{
nod tmp,ptr;
tmp=malloc(sizeof(struct node));
if(tmp==NULL)
printf("\nInsertion not possible!");
else
{
ptr=last;
while(ptr->info!=key && ptr->link!=last)
{
ptr=ptr->link;
}
if(ptr->link==last)
printf("\n%d not found!",key);
else if(ptr->info==key)
{
tmp->link=ptr->link;
tmp->info=data;
ptr->link=tmp;
disp();
}
}
return;
}
display()
{
nod q,c,p,t;
//int t;
if(last->link==last)
printf("\nList empty");
for(q=last->link;q!=last;q=q->link)
{
for(c=q->link;c!=last;c=c->link)
{
if(q->info > c->info)
{
t=q->info;
q->info=c->info;
c->info=t;
}
}
}
q=last->link;
printf("->");
while(q!=last)
{
printf("%d->",q->info);
q=q->link;
}
//printf("%d",q->link);
return;
}
dele()
{
nod ptr,ptr1;
ptr=last;
while(ptr->link!=last)
{
ptr1=ptr;
ptr=ptr->link;
}
ptr1->link=ptr->link;
free(ptr);
disp();
return;
}
delf()
{
nod prev,cur,q;
if(last->link==last)
printf("\nLIst empty");
else
{
prev=last->link;
cur=prev->link;
last->link=cur;
//cur->link=NULL;
free(prev);
disp();
}
return;
}
dela(int m)
{
nod ptr,ptr1;
ptr=last;
ptr1=ptr->link;
while(ptr->link!=last && ptr->info!=m)
{
ptr1=ptr;
ptr=ptr->link;
}
if(ptr->info==m)
{
ptr1->link=ptr->link;
free(ptr);
disp();
}
else
printf("\n%d not found!",m);
return;
}
modify(int key,int data)
{
nod prev,cur;
cur=last;
while(cur->link!=last && cur->info!=key)
{
prev=cur;
cur=cur->link;
}
if(cur->info==key)
cur->info=data;
if(cur->link==last && cur->link!=key)
printf("\n%d not found!",key);
else
disp();
return;
}