0
13kviews
Implement a menu-driven program to perform the queue operations like insert, delete, display, isempty, and isfull using array in C language.
1 Answer
1
226views

Menu-driven Queue Program

  • The Queue is Abstract Data Type in which addition of an element to the collection performed at the Rear terminal position called ENQUEUE and removal of an element performed at Front terminal position called DEQUEUE.

  • Hence, Queue follows a First-In-First-Out (FIFO) data structure, the first element added to the structure must be the first one to be removed.


Menu-driven C Program to Implement Queue Operations using Array -

#include<stdio.h>
#include<conio.h>
#define max 20
int q[max];
int front=-1,rear=-1;
void ins(int);
int del();
void display();
int isempty();
int isfull();
void main()
{
int ch,item,x;
char a;
printf("\n\t Queue Implementation");
printf("\n\t --------------------");
printf("\n\t1.Insert");
printf("\n\t2.Delete");
printf("\n\t3.Display");
printf("\n\t4.IsEmpty");
printf("\n\t5.IsFull");
do{
printf("\n\t Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter Element to Insert:\n");
       scanf("%d",&item);
       ins(item);
       break;
case 2:x=del();
       printf("The Element Deleted from Queue is %d",x);
       break;
case 3:display();
       break;
case 4:x=isempty();
       if(x==1)
       printf("Queue is Empty");
       else
       printf("Queue is Not Empty");
       break;
case 5:x=isfull();
       if(x==1)
       printf("Queue is Full");
       else
       printf("Queue is Not Full");
       break;
default:printf("INVALID Choice\n");
}
printf("\n do u want to continue y/n: ");
scanf(" %c",&a);
}while((a=='y')||(a=='Y'));
getch();
}

void ins(int x)           // Insert function
{
if(rear==max-1)
printf("Queue is OVERFLOW \n");
else if(rear==-1)
  {
   front=0;rear=0;
   q[rear]=x;
  }
else
  {
rear++;
q[rear]=x;
  }
}

int del()           // Delete function
{
int x;
if(front==-1)
printf("Queue is UNDERFLOW \n");
else if(front==rear)
 {
 x=q[front];
 front=-1;
 rear=-1;
 return(x);
 }
 else
 {
 x=q[front];
 front++;
 return(x);
 }
 }

void display()           // Display function
{
int i;
if(rear==-1)
printf("\n Queue is Empty");
else
{
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
}

int isempty()           // isempty function
{
if(front==-1)
return 1;
else
return 0;
}

int isfull()           // isfull function
{
if(rear==max-1)
return 1;
else
return 0;
}

Output of the Program -

Inititally the Queue is empty therefor program will show following output for the operations display, isempty, and isfull.

1

The below output shows the ENQUEUE or insert operation in the Queue that inserts 5 elements 10, 20, 30, 40, and 50 into the queue.

2

The below ouput shows the DEQUEUE or delete operation in the Queue that delet the front end element from the queue which are 10 and 20 in a sequence in this case.

3

Now, here at this point isempty and isfull operation show following output.

4

Finally, Queue is full with its MAX allocated size, here in program it is 20. Therefore, insert and isfull operations show the below output now.

5

Please log in to add an answer.