Write a menu driven program to implement queue operations
such as Enqueue, Dequeue, Peek, Display of elements, isEmpty,
isFull using static array.
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int queue[MAX];
int rear=-1;
int front=-1;
int isFull()
{
if( rear==MAX-1 )
return 1;
else
return 0;
}
int isEmpty()
{
if( front==-1 || front==rear+1 )
return 1;
else
return 0;
}
void enqueue(int n){
if( isFull() )
{
printf("\nOverflow\n");
return;
}
if( front == -1 )
front=0;
rear=rear+1;
queue[rear]=n ;
}
int dequeue(){
int i;
if( isEmpty() )
{
printf("\nUnderflow\n");
exit(1);
}
i=queue[front];
front=front+1;
return i;
}
int peek()
{
if( isEmpty() )
{
printf("\nUnderflow\n");
exit(1);
}
return queue[front];
}
void display()
{
int i;
if ( isEmpty() )
{
printf("\nQueue is empty\n");
return;
}
printf("\nQueue is :\n");
for(i=front;i<=rear;i++)
printf("%d ",queue[i]);
printf("\n\n");
}
int main()
{
int c,i;
while(1)
{
printf("\n1.Enqueue\n");
printf("2.Dequeue\n");
printf("3.Peek\n");
printf("4.Display \n");
printf("5.Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nInput the element for adding in queue : ");
scanf("%d",&i);
enqueue(i);
break;
case 2:
i=dequeue();
printf("\nDeleted element is %d\n",i);
break;
case 3:
printf("\nElement at the front is %d\n",peek());
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\nInvalid choice\n");
}
}
return 0;
}
Comments
Leave a comment