/* queues */
# include "conio.h"
# include "stdio.h"
struct node
{ struct node *prev;
int num;
struct node *next;
}*top=NULL,*ptr,*bottom=NULL;
main()
{
int ch;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Insert into queue");
printf("\n\t\t2. Delete from queue");
printf("\n\t\t3. Display queue details");
printf("\n\t\t4. Exit program");
printf("\n\t\Enter your choice(1-4)");
scanf("%d",&ch);
switch(ch)
{ case 1:
ins();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
ins()
{ if(top==NULL)
{
printf("in 1");
ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
top=ptr;
printf("top=%u",top);
bottom=ptr;
printf("bottom=%u",bottom);
printf("\nEnter a Number");
scanf("%d",&ptr->num);
ptr->prev=ptr->next=NULL;
printf("ptr prev=%d, ptr next=%d",ptr->prev,ptr->prev);
}
else
{
printf("in 2");
ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
printf("\nEnter a Number");
scanf("%d",&ptr->num);
ptr->next=top;
printf("ptr next=%d",ptr->next);
top=ptr;
printf("top =%u",top);
ptr->next->prev=top;
printf("ptr-next-prev=%d",ptr->next->prev);
ptr->prev=NULL;
printf("ptr->prev=%d",ptr->prev);
}
}
del()
{
if(bottom==NULL)
printf("\nQueue is empty");
else
{
printf("bottom=%u",bottom);
ptr=bottom;
printf("ptr=%u",ptr);
printf("ptr prev=%d",ptr->prev);
bottom=ptr->prev;
printf("ptr bottom=%u",bottom);
printf("\nDeleting node:%d",ptr->num);
free(ptr);
printf("bottom next=%d",bottom->next);
bottom->next=NULL;
}
}
disp()
{
ptr=bottom;
if(ptr==NULL)
printf("\nQueue is empty");
else
while(ptr!=NULL)
{
printf("\n%d",ptr->num);
ptr=ptr->prev;
}
}
# include "conio.h"
# include "stdio.h"
struct node
{ struct node *prev;
int num;
struct node *next;
}*top=NULL,*ptr,*bottom=NULL;
main()
{
int ch;
while(1)
{
clrscr();
printf("\n\t\tMain Menu");
printf("\n\t\t1. Insert into queue");
printf("\n\t\t2. Delete from queue");
printf("\n\t\t3. Display queue details");
printf("\n\t\t4. Exit program");
printf("\n\t\Enter your choice(1-4)");
scanf("%d",&ch);
switch(ch)
{ case 1:
ins();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
return;
default:
printf("\nWrong choice");
}
getch();
}
}
ins()
{ if(top==NULL)
{
printf("in 1");
ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
top=ptr;
printf("top=%u",top);
bottom=ptr;
printf("bottom=%u",bottom);
printf("\nEnter a Number");
scanf("%d",&ptr->num);
ptr->prev=ptr->next=NULL;
printf("ptr prev=%d, ptr next=%d",ptr->prev,ptr->prev);
}
else
{
printf("in 2");
ptr=(struct node *) malloc(sizeof(struct node));
printf("ptr=%u",ptr);
printf("\nEnter a Number");
scanf("%d",&ptr->num);
ptr->next=top;
printf("ptr next=%d",ptr->next);
top=ptr;
printf("top =%u",top);
ptr->next->prev=top;
printf("ptr-next-prev=%d",ptr->next->prev);
ptr->prev=NULL;
printf("ptr->prev=%d",ptr->prev);
}
}
del()
{
if(bottom==NULL)
printf("\nQueue is empty");
else
{
printf("bottom=%u",bottom);
ptr=bottom;
printf("ptr=%u",ptr);
printf("ptr prev=%d",ptr->prev);
bottom=ptr->prev;
printf("ptr bottom=%u",bottom);
printf("\nDeleting node:%d",ptr->num);
free(ptr);
printf("bottom next=%d",bottom->next);
bottom->next=NULL;
}
}
disp()
{
ptr=bottom;
if(ptr==NULL)
printf("\nQueue is empty");
else
while(ptr!=NULL)
{
printf("\n%d",ptr->num);
ptr=ptr->prev;
}
}
No comments:
Post a Comment