#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. Sort");
printf("\n 6. 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;
case 5:
sort();
getch();
continue;
}
}while(ch!= 6);
}
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 = NULL;
return;
}
prev->link = new;
new->link = NULL;
prev = new;
return;
}
display()
{
struct abc *x;
if(f==NULL && prev == NULL)
{
printf("\n Linked List is Empty");
return;
}
x=f;
while(x!=NULL)
{
printf("\n Value is : %d", x->info);
x = x->link;
}
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;
return;
}
p=f;
while(p!=NULL && p->info != x)
{
q=p;
p=p->link;
}
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;
while(p!=NULL)
{
if(p->info == x)
{
flag = 1;
break;
}
p=p->link;
}
if(flag)
printf("\n Element found in the list ");
else
printf("\n Element not found in the list ");
return;
}
sort()
{
struct abc *p,*q;
int x;
if(f==NULL && prev == NULL)
{
printf("\n Linked List is Empty");
return;
}
for(p=f;p!=NULL;p=p->link)
{
for(q=p;q!=NULL;q=q->link)
{
if(p->info > q->info)
{
x = p->info;
p->info = q->info;
q->info = x;
}
}
}
display();
return;
}
No comments:
Post a Comment