LinkList : Linklist
is way to store data in computer, this method organizing the stored data in a
computer's memory. All stored data records are assigned a physical address in
memory that the computer uses to locate the information. A linked list is a
data structure consisting of a group of nodes which together represent a
sequence. Each node in a linklist is composed of a data and a self reference
pointer that also know as link to stored the memory address of the next node in
the sequence.
Menu Driven Program for Single LinkList
#include <stdio.h>
#include <conio.h>
struct
node
{
int
n;
struct
node *next;
};
struct
node *head,*ptr;
void
Add(struct node *tmp)
{
if(head==NULL)
{
head=ptr=tmp;
}
else
{
ptr->next=tmp;
ptr=tmp;
}
}
void
Disp()
{
struct
node *pt;
clrscr();
printf("\n\tData
in the List : ");
for(pt=head;pt!=NULL;pt=pt->next)
{
printf("%d ",pt->n);
}
printf("\n\n\tPress
Any Key to Cont..");
getch();
}
void
Del ()
{
int
num;
struct
node *pt,*pt1;
printf("\n\tEnter
No. to Delete : ");
scanf("%d",&num);
for(pt=head;pt!=NULL;pt=pt->next)
{
if(pt->n==num)
{
break;
}
else
{
pt1=pt;
}
}
if(pt==head)
{
head=head->next;
}
else
if(pt==NULL)
{
printf("\n\tNumber
is not in the list \n");
}
else
{
pt1->next=pt->next;
}
free(pt);
}
void
search(int num)
{
int flag=0;
struct node *pt;
for(pt=head;pt!=NULL;pt=pt->next)
{
if(pt->n==num)
{
flag++;
}
}
if(flag>0)
{
printf("\n\t%d
is found! Repeated %d nos ",num,flag);
}
if(flag==0)
{
printf("\n\t%d
is not found!",num);
}
getch();
}
void
InsBefore(struct node *tmp,int pos)
{
int
i=0;
struct
node *pt1,*pt=head;
while(pt->next!=NULL)
{
i++;
if(i==pos)
{
break;
}
else
{
pt1=pt;
}
pt=pt->next;
}
if(pt==head)
{
tmp->next=head;
head=tmp;
}
else
if(pt->next==NULL)
{
pt->next=tmp;
}
else
{
tmp->next=pt1->next;
pt1->next=tmp;
}
}
void
InsAfter(struct node *tmp,int pos)
{
int
i=0;
struct
node *pt=head;
while(pt->next!=NULL)
{
i++;
if(i==pos)
{
break;
}
pt=pt->next;
}
if(pt==head)
{
tmp->next=head->next;
head->next=tmp;
}
else
if(pt->next==NULL)
{
pt->next=tmp;
}
else
{
tmp->next=pt->next;
pt->next=tmp;
}
}
void
Update()
{
struct node *pt;
int num,num1,flag=0;
printf("\n\tEnter Number to
Update : ");
scanf("%d",&num);
for(pt=head;pt!=NULL;pt=pt->next)
{
if(num==pt->n)
{
printf("\n\tEnter
Number to Replace : ");
scanf("%d",&num1);
pt->n=num1;
flag=1;
break;
}
}
if(flag==0)
{
printf("\n\t
No. is not found !!");
}
else
{
printf("\n**
No. is Updated ** ");
}
getch();
}
void
reverse()
{
struct
node *pt,*pt1,*pt2;
pt=head;
pt1=NULL;
while(pt!=NULL)
{
pt2=pt1;
pt1=pt;
pt=pt->next;
pt1->next=pt2;
}
head=pt1;
}
void
Sort()
{
struct
node *pt,*pt1;
int
num;
clrscr();
for(pt=head;pt->next!=NULL;pt=pt->next)
{
for(pt1=pt->next;pt1!=NULL;pt1=pt1->next)
{
if(pt->n>pt1->n)
{
num=pt->n;
pt->n=pt1->n;
pt1->n=num;
}
}
}
}
void main()
{
struct node *temp;
int ch=0,pos;
char ch1;
clrscr();
head=temp=ptr=NULL;
while(ch!=9)
{
clrscr();
printf("\n\t* * Main
Menu * *");
printf("\n\t1. Add
");
printf("\n\t2.
Display");
printf("\n\t3.
Delete");
printf("\n\t4.
Insert");
printf("\n\t5.
Search");
printf("\n\t6.
Update");
printf("\n\t7.
Reverse");
printf("\n\t8.
Sort");
printf("\n\t9.
Exit");
printf("\n\tEnter a
Choice : ");
scanf("%d",&ch);
switch(ch)
{
case
1:
temp=(struct
node *)malloc(sizeof(struct node));
printf("\n\tEnter Data : ");
scanf("%d",&temp->n);
temp->next=NULL;
Add(temp);break;
case
2:
Disp();break;
case
3:
case
4:
printf("\n\tInsert
(b)efore/(a)fter? ");
ch1=getche();
if(ch1=='b'
|| ch1=='B')
{
temp=(struct
node *)malloc(sizeof
(struct
node));
printf("\n\tEnter Data : ");
scanf("%d",&temp->n);
temp->next=NULL;
printf("\n\tEnter
Position : ");
scanf("%d",&pos);
InsBefore(temp,pos);break;
}
else
if(ch1=='a' || ch1=='A')
{
temp=(struct
node *)malloc(sizeof
(struct
node));
printf("\n\tEnter Data : ");
scanf("%d",&temp->n);
temp->next=NULL;
printf("\n\tEnter
Position : ");
scanf("%d",&pos);
InsAfter(temp,pos);
}
else
{
printf("\n\tWrong
Choice");
}
break;
case
5:
printf("\n\tEnter
No. to Search : ");
scanf("%d",&pos);
search(pos);
break;
case
6:
Update();break;
case
7:
reverse();break;
case
8:
Sort();break;
case
9:
printf("\n\t\tQuit
....");getch();
exit(0);
default:printf("\n\t\tInvalid
Choice ");
}
}
getch();
}
No comments:
Post a Comment