Sunday, March 18, 2012

queue

#include
#include
#include
#include

struct queue
{
int data;
struct queue*next;
};

void main()
{
struct queue*front=NULL,*rear=NULL,*temp,*trav;
int choice;

clrscr();
while(1)
{
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(rear==NULL)
{
front=(struct queue*)malloc(sizeof(struct queue));
printf("\nEnter Data");
scanf("%d",&front->data);
front->next=NULL;
rear=front;
}
else
{
temp=(struct queue*)malloc(sizeof(struct queue));
printf("\nEnter Data");
scanf("%d",&temp->data);
temp->next=NULL;

rear->next=temp;
rear=temp;
}
break;
case 2:
if(front!=rear)
{
printf("Element %d deleted !!",front->data);
front=front->next;
}
else
{
if(front!=NULL)
{
printf("Element %d deleted !!",front->data);
front=NULL;
rear=NULL;
}
else
{
printf("\nQueue is EMPTY !!");
}
}
break;
case 3:
trav=front;
while(trav!=NULL)
{
printf("%d ",trav->data);
trav=trav->next;
}
break;
case 4:
exit(0);
}
}

getch();

}

No comments:

Post a Comment