This is a program on queue using doubly linked list.
# include< stdio.h>
# include< stdlib.h>
struct link
{
int info;
struct link *next;
struct link *prev;
};
struct link *p;
void display(struct link *rec)
{
while(rec != NULL)
{
printf(" %d ",rec->info);
rec = rec->next;
}
}
struct link *insert(struct link *node)
{
struct link *xx,*new1;
xx=node;
p=NULL;
new1 = (struct link *) malloc(sizeof(struct link));
fflush(stdin);
printf("\nEnter the value: ");
scanf("%d", &new1->info);
if (node == NULL)
{
node->next=new1;
new1->prev=node;
new1->next=NULL;
node=new1;
}
else
{
while(xx)
{
xx = xx->next;
p=p->next;
}
p->next = new1;
new1->prev = p;
new1->next = xx;
}
return node;
}
struct link * del(struct link *rec)
{
struct link *temp;
if(rec == NULL)
{
printf("\n Empty");
}
else
{
temp = rec->next;
rec->next->prev=temp;
free(rec);
rec = temp;
printf("\n After deletion the queue is as follows:\n");
display(rec);
if(rec == NULL)
printf("\n Queue is empty");
}
return rec;
}
int selection()
{
int choice;
do
{
printf("\n 1<-Push ");
printf("\n 2<-Pop");
printf("\n 3<-Quit");
printf("\n Input your choice :");
scanf("%d", &choice);
if(choice <1 || choice >3)
printf("\n Incorrect choice-> Try once again");
} while(choice <1 || choice >3);
return choice;
}
void main()
{
struct link *start ;
int choice;
clrscr();
start = NULL;
do
{
choice = selection();
switch(choice)
{
case 1:
start = insert(start);
printf("\n After insertion queue is as follows:\n");
display(start);
break;
case 2:
start = del(start);
break;
default :
printf("\n End of session");
}
} while(choice != 3);
getch();
}
No comments:
Post a Comment