You are on page 1of 4

PROGRAM:-4

Write a program to implement a queue.

# include <stdio.h>
# define size 10
int front,rear;
void push();
int pop();
void display();
int queue[size];
void main()
{
int choice;
char ch='y';
front=-1,rear=-1;
while(ch=='y' || ch=='Y')
{
clrscr();
printf("\n1.\tPush\n2.\tPop\n3.\tDisplay\n\n");
printf("Enter your Choice (1-3]");
scanf("%d",&choice);
switch(choice)
{
case 1 : push(); break;
case 2 :
printf("\nDeleted Item is =%d",pop());
break;
case 3 : display(); break;
default :
printf("\nWrong Choice[1-3");
}
printf("\nDo You Want to COntinue(y/n)");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}
void push()
{
int item;
if(rear==size-1)
{
printf("\nQueue is Full :");
exit();
}
else
{
printf("Enter the Elements to be Inserted :");
scanf("%d",&item);
rear++;
queue[rear]=item;
}
}
void display()
{
int a;
printf("\nThe Elements of Queue is \n\n");
printf("Front-->");
for(a=front;a<=rear;a++)
printf("%d\t",queue[a]);
printf("<--Rear");
}
int pop()
{
int item;
if(rear==-1)
{
printf("\nQueue is Empty :");
exit();
}
else
{
front++;
item=queue[front];
if(front==rear)
{
rear=-1;
front=-1;
}
return item;
}
}
OUTPUT

You might also like