Apr 2, 2011

Write a program to perform all operations on singly Circular linked list

#include
#include
struct abc
{
int info;
struct abc *link;
};
struct abc *f = NULL, *prev = NULL;
void main()
{
int ch;
do
{
clrscr();
printf("\n Linked list");
printf("\n 1. Insert");
printf("\n 2. Display");
printf("\n 3. Delete ");
printf("\n 4. Search");
printf("\n 5. Exit");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
getch();
continue;
case 2:
display();
getch();
continue;
case 3:
delete();
getch();
continue;
case 4:
search();
getch();
continue;
}
}while(ch!= 5);
}
insert()
{
struct abc *new;
new = (struct abc *) malloc(sizeof(struct abc));
printf("\n Enter value :");
scanf("%d",&new->info);
if(f==NULL && prev == NULL)
{
f=new;
prev= new;
new->link = f;
return;
}
prev->link = new;
new->link = f;
prev = new;
return;
}
display()
{
struct abc *x;
if(f==NULL && prev == NULL)
{
printf("\n Linked List is Empty");
return;
}
x=f;
do
{
printf("\n Value is : %d", x->info);
x = x->link;
}while(x!=f);
return;
}
delete()
{
struct abc *p,*q;
int x;
if(f==NULL && prev == NULL)
{
printf("\n Linked List is Empty");
return;
}
printf("\n Enter element to delete : ");
scanf("%d",&x);
if(f->info == x && f == prev)
{
f = NULL;
prev = NULL;
return;
}
if(f->info == x)
{
f = f->link;
prev->link = f;
return;
}
p=f;
do
{
q=p;
p=p->link;
}while(p!=f && p->info != x);

if(p->info == x && p == prev)
{
q->link = p->link;
prev = q;
return;
}
if(p->info == x)
{
q->link = p->link;
return;
}
printf("\n Element not found in the list ");
return;
}
search()
{

struct abc *p,*q;
int x,flag=0;
if(f==NULL && prev == NULL)
{
printf("\n Linked List is Empty");
return;
}
printf("\n Enter element to search : ");
scanf("%d",&x);
p=f;
do
{
if(p->info == x)
{
flag = 1;
break;
}
p=p->link;
}while(p!= f);
if(flag)
printf("\n Element found in the list ");
else
printf("\n Element not found in the list ");
return;
}

No comments:

Post a Comment