#include<malloc.h>
void disp();
void create(int data);
void insertbeg(int data);
void insertbet(int data,int pos);
void del(int data);
void count();
void search(int data);
struct node
{
int info;
struct node *link;
}*last=NULL;
main()
{
int m,pos,i,n,ch;
while(1)
{
printf("\n\n\n........MENU..........\n");
printf("\n1.create list\n");
printf("2.dispaly\n");
printf("3.insert at bigining\n");
printf("4.insert in between\n");
printf("5.delete\n");
printf("6.count\n");
printf("7.search\n");
printf("8.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("how many nodes:");
scanf("%d",&n);
printf("enter the element:\n");
for(i=0;i<n;i++)
{
scanf("%d",&m);
create(m);
}
break;
case 2:
disp();
break;
case 3:
printf("enter element:");
scanf("%d",&m);
insertbeg(m);
break;
case 4:
printf("enter element:");
scanf("%d",&m);
printf("enter the position:");
scanf("%d",&pos);
insertbet(m,pos);
break;
case 5:
printf("enter the element:");
scanf("%d",&m);
del(m);
break;
case 6:
count();
break;
case 7:
printf("enter the element you want to search:");
scanf("%d",&m);
search(m);
break;
case 8:
exit(1);
}
}
}
void create(int data)
{
struct node *q,*tmp;
tmp=malloc(sizeof(struct node ));
tmp->info=data;
if(last==NULL)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}
return;
}
void disp()
{
struct node *q;
if(last==NULL)
{
printf("list is empty\n");
return;
}
printf("the list is:\n");
q=last->link;
while(q!=last)
{
printf("%d-->",q->info);
q=q->link;
}printf("%d",last->info);
}
void insertbeg(int data)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
void insertbet(int data,int pos)
{
struct node *q,*tmp;
int i;
q=last;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q->link==last)
{
printf("there is less than %d element\n",pos);
return;
}
}
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=q->link;
q->link=tmp;
}
void del(int data)
{
struct node *tmp,*q;
if((last->link==last)&&(last->info==data))
{
tmp=last;
last=NULL;
free(tmp);
}
q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
return;
}
q=last;
while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
}
q=q->link;
}
if(q->link->info==data)
{
tmp=q->link;
q->link=last->link;
free(tmp);
last=q;
return;
}
}
void count()
{
struct node *q;
int cnt=0;
if(last==NULL)
printf("list is empty\n");
else
{
q=last;
do
{
cnt=cnt+1;
q=q->link;
}while(q!=last);
printf("number of items=%d\n",cnt);
}
}
void search(int data)
{
struct node *q;
if(last==NULL)
printf("list is empty\n");
else
{
q=last;
do
{
if(q->info==data)
{
printf("element is fount\n");
return;
}
q=q->link;
}while(q!=last);
printf("searched element is not found\n");
return;
}
}
#include<malloc.h>
void disp();
void create(int data);
void insertbeg(int data);
void insertbet(int data,int pos);
void del(int data);
void count();
void search(int data);
struct node
{
int info;
struct node *link;
}*last=NULL;
main()
{
int m,pos,i,n,ch;
while(1)
{
printf("\n\n\n........MENU..........\n");
printf("\n1.create list\n");
printf("2.dispaly\n");
printf("3.insert at bigining\n");
printf("4.insert in between\n");
printf("5.delete\n");
printf("6.count\n");
printf("7.search\n");
printf("8.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("how many nodes:");
scanf("%d",&n);
printf("enter the element:\n");
for(i=0;i<n;i++)
{
scanf("%d",&m);
create(m);
}
break;
case 2:
disp();
break;
case 3:
printf("enter element:");
scanf("%d",&m);
insertbeg(m);
break;
case 4:
printf("enter element:");
scanf("%d",&m);
printf("enter the position:");
scanf("%d",&pos);
insertbet(m,pos);
break;
case 5:
printf("enter the element:");
scanf("%d",&m);
del(m);
break;
case 6:
count();
break;
case 7:
printf("enter the element you want to search:");
scanf("%d",&m);
search(m);
break;
case 8:
exit(1);
}
}
}
void create(int data)
{
struct node *q,*tmp;
tmp=malloc(sizeof(struct node ));
tmp->info=data;
if(last==NULL)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}
return;
}
void disp()
{
struct node *q;
if(last==NULL)
{
printf("list is empty\n");
return;
}
printf("the list is:\n");
q=last->link;
while(q!=last)
{
printf("%d-->",q->info);
q=q->link;
}printf("%d",last->info);
}
void insertbeg(int data)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
void insertbet(int data,int pos)
{
struct node *q,*tmp;
int i;
q=last;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q->link==last)
{
printf("there is less than %d element\n",pos);
return;
}
}
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=q->link;
q->link=tmp;
}
void del(int data)
{
struct node *tmp,*q;
if((last->link==last)&&(last->info==data))
{
tmp=last;
last=NULL;
free(tmp);
}
q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
return;
}
q=last;
while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
}
q=q->link;
}
if(q->link->info==data)
{
tmp=q->link;
q->link=last->link;
free(tmp);
last=q;
return;
}
}
void count()
{
struct node *q;
int cnt=0;
if(last==NULL)
printf("list is empty\n");
else
{
q=last;
do
{
cnt=cnt+1;
q=q->link;
}while(q!=last);
printf("number of items=%d\n",cnt);
}
}
void search(int data)
{
struct node *q;
if(last==NULL)
printf("list is empty\n");
else
{
q=last;
do
{
if(q->info==data)
{
printf("element is fount\n");
return;
}
q=q->link;
}while(q!=last);
printf("searched element is not found\n");
return;
}
}
OUTPUT
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:1
how many nodes:5
enter the element:
1
2
3
4
5
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
1-->2-->3-->4-->5
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:3
enter element:6
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
6-->1-->2-->3-->4-->5
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:4
enter element:7
enter the position:5
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
6-->1-->2-->3-->7-->4-->5
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:5
enter the element:7
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:2
the list is:
6-->1-->2-->3-->4-->5
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:6
number of items=6
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:7
enter the element you want to search:8
searched element is not found
........MENU..........
1.create list
2.dispaly
3.insert at bigining
4.insert in between
5.delete
6.count
7.search
8.exit
enter your choice:8
Process returned 1 (0x1) execution time : 94.079 s
Press any key to continue.