#include
struct node
{
int data;
struct node *next;
struct node *pre;
};
struct node *front,*rear;
void main()
{
int num,option;
char choice='y';
void add(int num);
int r();
void display();
clrscr();
front=rear=NULL;
while(choice=='y')
{
printf("\n\n1.ADD");
printf("\n\n2.REMOVE");
printf("\n\n3.DISPLAY");
printf("\n\nEnter the option:");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\n\nEnter The no.:");
scanf("%d",&num);
add(num);
printf("\n\nDo you want to continue:");
break;
case 2:
num=r();
printf("\n\nRemove item=%d",num);
printf("\n\nDo you want to continue:");
break;
case 3:
display();
printf("\n\nDo you want to continue:");
break;
default :
printf("\n\nInvalid option");
printf("\n\nDo you want to continue:");
}
choice=getch();
}
getch();
}
void add(int num)
{
if(front==NULL &&rear==NULL)
{
front=rear=(struct node *)malloc(sizeof(struct node));
rear->data=num;
rear->next=NULL;
rear->pre=NULL;
}
else
{
rear->next=(struct node *)malloc(sizeof(struct node));
rear->next->data=num;
rear->next->next=NULL;
rear->next->pre=rear;
rear=rear->next;
}
}
int r()
{
int result;
struct node *q=front;
if(front==NULL &&rear==NULL)
{
printf("\n\nQueue is empty");
return -32768;
}
else
{
result=front->data;
front=front->next;
front->pre=NULL;
q->next=NULL;
free(q);
if(front==NULL)
{
rear=NULL;
}
return(result);
}
}
void display()
{
struct node *q=front;
if(front==NULL &&rear==NULL)
{
printf("\n\nQueue is empty");
}
else
{
while(q!=NULL)
{
printf("%d,",q->data);
q=q->next;
}
}
}
No comments:
Post a Comment