Pages

Monday, January 3, 2011

Double Linked List : Source Code

#include
#include
#include
struct viewer
{
int seatno;
char vname[20];
char address[30];
viewer *next;
viewer *prior;
}*start,*end; // To point to the first and last node respectively.

void main()
{
start=NULL;
end=NULL;
void add();
void traverse(); // from start to end
void rtraverse(); // from end to start
void search();
void del();
int c;
do
{
printf("\nDOUBLE LINKED LIST - MAIN MENU");
printf("\n1. Add a node");
printf("\n2. Print all nodes");
printf("\n3. Print Reverse");
printf("\n4. Search");
printf("\n5. Delete");
printf("\n6. Quit");
printf("\nSelect your choice ");
scanf("%d",&c);
if(c==1)
add();
else if (c==2)
traverse();
else if(c==3)
rtraverse();
else if(c==4)
search();
else if(c==5)
del();
}while(c!=6);
}
void add()
{
clrscr();
viewer *v;
printf("\n A D D  A  N O D E ");
v=(viewer *)malloc(sizeof(viewer));
printf("\nEnter Seat No ");
scanf("%d",&v->seatno);
fflush(stdin);
printf("\nEnter Name ");
gets(v->vname);
fflush(stdin);
printf("\nEnter Address ");
gets(v->address);
if(start==NULL)
{
start=v;
end=v;
v->next=NULL;
v->prior=NULL;
}
else
{
v->prior=end;
end->next=v;
v->next=NULL;
end=v;
}

}
void traverse()
{
clrscr();
viewer *v;
if(start==NULL)
{
printf("\nNo Nodes Added Yet...");
return;
}
for(v=start;(v);v=v->next)
{
printf("\nSeat No. : %d",v->seatno);
printf("\nName of the viewer : %s",v->vname);
printf("\nAddress of the viewer : %s",v->address);
printf("\n---------------------------");
}
printf("\nEnd of list");
printf("\nPress enter to continue..");
getch();
clrscr();
}
void rtraverse()
{
viewer *v;
for(v=end;(v);v=v->prior)
{
printf("\nSeat No. : %d",v->seatno);
printf("\nName of the viewer : %s",v->vname);
printf("\nAddress of the viewer : %s",v->address);
printf("\n---------------------------");
}
}
void search()
{
clrscr();
printf("\nSearch a node");
int sno,flag;
char ch;
viewer *v;
if(start==NULL)
{
printf("\nNo Node Found");
return;
}
printf("\nEnter Seat No To Search : ");
scanf("%d",&sno);
for(v=start;(v);v=v->next)
{
if(v->seatno==sno)
{
flag=1;
printf("\nSeat no Found");
printf("\n=============");
printf("\nSeat No.: %d",v->seatno);
printf("\nName of the viewer : %s",v->vname);
printf("\nAddress of the viewer : %s",v->address);
fflush(stdin);
printf("\nDo you want to modify the data? : ");
scanf("%c",&ch);
if(ch=='y'  || ch=='Y')
{
printf("\nEnter New seat no "); // overwriting the data
scanf("%d",&v->seatno);
fflush(stdin);
printf("\nEnter New Name ");
gets(v->vname);
fflush(stdin);
printf("\nEnter new Address ");
gets(v->address);
printf("\nRecord Updated !!");
}
}


}


if(flag!=1)
printf("\nSeat no %d not found",sno);
}
void del()
{
clrscr();
printf("\nDelete a node");
viewer *v,*prev;
int sno,flag;
char ch;
if(start==NULL)
{
printf("\nNo node Found");
return;
}
printf("\nEnter Seat no To Delete : ");
scanf("%d",&sno);
for(v=start;(v);v=v->next)
{
if(v->seatno==sno)
{
flag=1;
printf("\nSeat no. Found");
printf("\n=============");
printf("\nSeat no : %d",v->seatno);
printf("\nName of the viewer : %s",v->vname);
printf("\nAddress of the viewe : %s",v->address);
fflush(stdin);
printf("\nDo you want to delete the node? : ");
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
if(v->next==NULL)
{prev->next=NULL;
end=prev;
}
else if(v==start)
{
start=v->next;
start->prior=NULL;
}
else
{
v->prior->next=v->next;
v->next->prior=v->prior;
}
free (v);
printf("\nNode Deleted");
}
}
prev=v;
}
if(flag!=1)
printf("\nSeat no %d not found",sno);
}

0 comments:

Post a Comment