#include <iostream>
#define size 8
using namespace std;
int array[size];
int front=-1,rear=-1;
int count=0;
void enqueue()
{
if(size==count)
{
cout<<"Memory is Full"<<endl;
return;
}
if(front==-1)
{
front++;
rear++;
}
else
{
rear = (rear +1) % (size+1);
}
cout<<"Data: ";
int data;
cin>>data;
array[rear] = data;
count++;
}
void dequeue()
{
if(count==0)
{
cout<<"Memory is Empty "<<endl;
return;
}
front = (front +1) % (size +1);
count--;
}
void push()
{
if(size==count)
{
cout<<"Memory is Full"<<endl;
return;
}
cout<<"Data: ";
int data;
cin>>data;
if(front == -1)
{
front++;
rear++;
}
else
{
rear = (rear +1) % (size+1);
}
array[rear] = data;
count++;
}
void pop()
{
if(count==0)
{
cout<<"Memory is Empty "<<endl;
return;
}
rear=(rear -1) % (size+1);
count--;
}
void print()
{
int c,i;
if(count==0)
{
cout<<"Memory is Empty"<<endl<<endl;
return;
}
cout<<"Your Memory: ";
for(i=front,c=1; c<=count; i = (i+1)%size,c++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}
void menu()
{
int n;
cout<<"Enter Total Entry: ";
cin>>n;
cout<<"Press E for EnqueuePress D for DequeuePress P for PushPress P for POP"<<endl;
while(n>0)
{
char choice;
cout<<"Your Choice: ";
cin>>choice;
if(choice == 'E')
{
enqueue();
}
if(choice == 'D')
{
dequeue();
}
if(choice == 'P')
{
push();
}
if(choice == 'O')
{
pop();
}
n--;
}
}
int main()
{
menu();
print();
return 0;
}
Output
Enter Total Entry: 12
Press E for Enqueue
Press D for Dequeue
Press P for Push
Press P for POP
Your Choice: D
Memory is Empty
Your Choice: P
Data: 7
Your Choice: P
Data: 9
Your Choice: E
Data: 13
Your Choice: P
Data: 17
Your Choice: D
Your Choice: E
Data: 19
Your Choice: D
Your Choice: P
Data: 23
Your Choice: O
Your Choice: O
Your Choice: E
Data: 18
Your Memory: 13 17 18
Add your comment