Monday, May 4, 2009

C-PROGRAM FOR QUEUE

#include
#include
#include
#define MAX 5
int q[MAX],rear=-1,front=-1;
char choice;
void insertion()
{
do
{
if(rear==MAX-1)
{
printf("OVER FLOW\n");
choice='n';
}
else
{
printf("Enter an element\n");
rear++;
scanf("%d",&q[rear]);
if(front==-1)
front++;
printf("PUSH ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}while(choice=='Y'choice=='y');
}
void deletion()
{
do
{
if(front==-1)
{
printf("\nUNDER FLOW\n");
choice='n';
}
else
{
if(front==rear)
{
printf("The poped element is %d\n",q[front]);
front=-1;
rear=-1;
}
else
{
printf("The poped element is %d\n",q[front]);
front++;
printf("POP ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}
}while(choice=='Y'choice=='y');
}
void display()
{
if(front==-1)
printf("\nUNDER FLOW\n");
else
{
printf("The elements present in the stack are\n");
while(front!=rear)
{
printf(" %d\n",q[front]);
front++;
}
if(front==rear)
{
printf(" %d\n",q[front]);
front=-1;
rear=-1;
}
}
}
void main()
{
int option;
clrscr();
do
{
printf("\nPress 1 for insertion\nPress 2 for deletion\nPress 3 to display the elements\nPress 4 to exit\nEnter your option:");
scanf("%d",&option);
switch(option)
{
case 1:insertion();
break;
case 2:deletion();
break;
case 3:display();
break;
case 4:exit(0);
break;
}
}while(option!=4);
}

No comments:

Post a Comment