0% found this document useful (0 votes)
29 views

Program:-4: Write A Program To Implement A Queue

The document describes a program to implement a queue data structure using an array. It defines functions for pushing an item onto the queue, popping an item off the queue, and displaying the current items in the queue. The main function uses a menu-driven loop to continually prompt the user to push, pop, or display items until they choose to quit.

Uploaded by

jindal112211
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Program:-4: Write A Program To Implement A Queue

The document describes a program to implement a queue data structure using an array. It defines functions for pushing an item onto the queue, popping an item off the queue, and displaying the current items in the queue. The main function uses a menu-driven loop to continually prompt the user to push, pop, or display items until they choose to quit.

Uploaded by

jindal112211
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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