#include<stdio.h>
int MAX=0;
int front=0;
int rear=-1;
int queue[100];
//Empty
int isEmpty()
{
if(front==0 && rear==-1)
return 1;
else
return 0;
}
//Full
int isFull()
{
if(front==0 && rear==MAX-1)
return 1;
else
return 0;
}
//Add
void add()
{
int value;
if(isFull())
{
printf("queue Overflow\n");
}
else
{
printf("Enter the value:");
scanf("%d",&value);
rear=(rear+1)%MAX;
queue[rear]=value;
printf("Value added\n");
}
}
//delete
void del()
{
if(isEmpty())
{
printf("queue Empty\n");
}
else
{
printf("Value removed:%d\n",queue[front]);
if(front!=rear)
{
front=(front+1)%MAX;
}
else
{
front=0;
rear=-1;
}
}
}
//Display
void display()
{
int i;
if(isEmpty())
{
printf("queue Empty\n");
}
else
{
printf("\nQueue:\n");
if(front<=rear)
{
for(i=front;i<=rear;i++)
{
printf("%d\n",queue[i]);
}
}
else if(rear<=front)
{
for(i=rear;i<=front;i++)
{
printf("%d\n",queue[i]);
}
}
}
}
//Front Rear
void display_front_rear()
{
if(isEmpty())
{
printf("Queue Empty\n");
}
else
{
printf("Front Element:%d\n",queue[front]);
printf("Rear Element:%d\n",queue[rear]);
}
}
//Main
int main()
{
char ch,choice;
printf("Enter the max length of the queue:");
scanf("%d",&MAX);
fflush(stdin);
printf("MENU:\nA)ADD\nB)REMOVE\nC)DISPLAY\nD)FRONT AND REAR ELEMENT\nE)EXIT\n");
scanf("%c",&choice);
fflush(stdin);
while(choice!='E')
{
switch(choice)
{
case 'A':
add();
break;
case 'B':
del();
break;
case 'C':
display();
break;
case 'D':
display_front_rear();
break;
default:
printf("Invalid Input");
}
fflush(stdin);
printf("MENU:\nA)ADD\nB)REMOVE\nC)DISPLAY\nD)FRONT AND REAR ELEMENT\nE)EXIT\n");
scanf("%c",&choice);
fflush(stdin);
}
printf("Thank You for using our service\n");
return 0;
}
Contributed By: Abhishek Banerjee, CSE, JISCE
Comments