-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcircular.cpp
112 lines (107 loc) · 2.16 KB
/
circular.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
#define max 5
using namespace std;
class Circular_Queue{
public:
int arr[max];
int front;
int rear;
Circular_Queue(){
front=-1;
rear=-1;
}
int is_empty(){
if(rear==-1){
return 1;
}else{
return 0;
}
}
int is_Full(){
if(front==(rear+1)%max){
return 1;
}else{
return 0;
}
}
void enqueue(int element){
if(is_Full()==1){
cout<<"Queue already full..overflow condition"<<endl;
return;
}
if(rear==-1){
front=(front+1)%max;
}
rear=(rear+1)%max;
arr[rear]=element;
}
void dequeue(){
if(is_empty()==1){
cout<<"Queue already empty..underflow condition"<<endl;
return;
}
if(front==rear){
front=-1;
rear=-1;
}else{
front=(front+1)%max;
}
}
void display(){
if(is_empty()==1){
cout<<"Empty queue.."<<endl;
return;
}
int i=front;
while(i!=(rear+1)%max){
cout<<arr[i]<<" ";
i=(i+1)%max;
}
}
int peek(){
if(is_empty()==1){
cout<<"Empty queue.."<<endl;
}else{
return arr[front];
}
}
};
int main() {
Circular_Queue cq;
int choice, element;
do {
cout << "\nMenu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display Queue\n";
cout << "4. Peek\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter element to enqueue: ";
cin >> element;
cq.enqueue(element);
break;
case 2:
cq.dequeue();
break;
case 3:
cq.display();
break;
case 4:
element = cq.peek();
if (element != -1) {
cout << "Front element: " << element << endl;
}
break;
case 5:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 5);
return 0;
}