#include<iostream>
using namespace std;
class CircularQueue {
private:
int front;
int rear;
int arr[5];
public:
CircularQueue() {
front = -1;
rear = -1;
for (int i = 0; i < 5; i++)
{
arr[i] = 0;
}
}
void enqueue(int val) {
if (rear==4 && front==0) {
cout << "Queue full" << endl;
return;
}
else if (front==-1 && rear==-1){
rear = 0;
front = 0;
arr[rear] = val;
}
else if(front!=0 && rear==4)
{
rear=0;
arr[rear]=val;
}else {
rear = (rear + 1) % 5;
arr[rear] = val;
}
}
int dequeue() {
int x = 0;
if (front==-1 && rear==-1) {
cout << "Queue is Empty" << endl;
return x;
} else if (rear == front) {
x = arr[rear];
rear = -1;
front = -1;
return x;
} else {
cout << "element delete from queue is: " <<arr[front] << endl;
x = arr[front];
arr[front] = 0;
front = (front + 1) % 5;
return x;
}
}
void display() {
cout << "All values in the Queue are - " << endl;
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
}
};
int main()
{
CircularQueue obj;
cout<<"\t1.Enquque"<<endl;
cout<<"\t2.Dequeue"<<endl;
cout<<"\t3.Display"<<endl;
int c;
while(true)
{
cout<<"please enter your choice:";
cin>>c;
switch(c)
{
case 1:
int no;
cout<<"please enter element in queue:";
cin>>no;
obj.enqueue(no);
break;
case 2:
obj.dequeue();
break;
case 3:
obj.display();
break;
default:
cout<<"wrong choice:"<<endl;
break;
}
}
}
No comments:
Post a Comment