#include <iostream>
#define MAX 5
using namespace std;
int array[MAX],array1[MAX];
int front1=-1,rear1=-1;
int front=-1,rear=-1,count=0;
void enqueue() ///enqueue element from this function
{
if(MAX==count) ///if the array is full
{
cout<<"Your Queue is Full"<<endl;
}
else
{
int data;
cout<<"Data: ";
cin>>data;
if(front==-1) ///for the 1st input
{
front++;
rear++;
}
else
{
rear = (rear+1)%MAX; /// increment rear by add 1 and then Mood the rear with Max
}
array[rear]=data; ///save data
count++;///increment count
}
}
void dequeue()
{
if(count==0) ///if the array is empty
{
cout<<"The Queue is Empty"<<endl<<endl;
return;
}
else
{
int count2=0;
while(count>=0)
{
if(front1==-1) ///for the 1st input
{
front1++;
rear1++;
}
else
{
rear1 = (rear1+1)%MAX;
}
array1[rear1]=array[rear]; ///save to another array
front = (front+1)%MAX;
rear= (rear-1)%MAX;
count2++;
count--;
}
count2--; ///delete last
count=0;
front=-1;
rear=-1;
while(count2>0) ///again save to the 1st array
{
if(front==-1) ///for the 1st input
{
front++;
rear++;
}
else
{
rear = (rear+1)%MAX; /// increment rear by add 1 and then Mood the rear with Max
}
array[rear]=array1[rear1]; ///save data
rear1 = (rear1-1)%MAX;
count++;
count2--;
}
count--;
front = (front +1)%MAX; /// increase the front value
}
}
void display()
{
int c,i;
if(count==0)///if the array is empty
{
cout<<"The Queue is Empty"<<endl<<endl;
return;
}
cout<<"Your Queue: ";
for(i=front,c=1; c<=count; i = (i+1)%MAX,c++) ///compare c with count and increase the i value and mood it by MAX
{
cout<<array[i]<<" ";
}
cout<<endl;
}
void menu()
{
int n;
while(1)
{
cout<<"Press 1 For EnqueuePress 2 For DequeuePress 3 For Display QueuePress 0 For Exit";
cin>>n;
if(n==1)
{
enqueue();
}
else if(n==2)
{
dequeue();
}
else if(n==3)
{
display();
}
else if(n==0)
{
return;
}
}
}
int main()
{
menu();
return 0;
}
Output
Press 1 For Enqueue
Press 2 For Dequeue
Press 3 For Display Queue
Press 0 For Exit
1
Data: 1
Press 1 For Enqueue
Press 2 For Dequeue
Press 3 For Display Queue
Press 0 For Exit
1
Data: 2
Press 1 For Enqueue
Press 2 For Dequeue
Press 3 For Display Queue
Press 0 For Exit
1
Data: 3
Press 1 For Enqueue
Press 2 For Dequeue
Press 3 For Display Queue
Press 0 For Exit
3
Your Queue: 1 2 3
Press 1 For Enqueue
Press 2 For Dequeue
Press 3 For Display Queue
Press 0 For Exit
2
Press 1 For Enqueue
Press 2 For Dequeue
Press 3 For Display Queue
Press 0 For Exit
3
Your Queue: 1 2
Press 1 For Enqueue
Press 2 For Dequeue
Press 3 For Display Queue
Press 0 For Exit
0
Add your comment