-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram8.cpp
More file actions
114 lines (94 loc) · 2.43 KB
/
Program8.cpp
File metadata and controls
114 lines (94 loc) · 2.43 KB
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
// Program to create a circular queue and implement insertion and deletion operations on it.
#include <iostream>
using namespace std;
#define MAX_SIZE 5 // Define the maximum size of the circular queue
class CircularQueue {
private:
int front, rear;
int arr[MAX_SIZE];
public:
CircularQueue() {
front = -1;
rear = -1;
}
bool isEmpty() {
return (front == -1);
}
bool isFull() {
return ((front == 0 && rear == MAX_SIZE - 1) || front == rear + 1);
}
void enqueue(int data) {
if (isFull()) {
cout << "Queue is full\n";
} else {
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX_SIZE;
}
arr[rear] = data;
}
}
int dequeue() {
if (isEmpty()) {
cout << "Queue is empty\n";
return -1;
} else {
int data = arr[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
return data;
}
}
void display() {
if (isEmpty()) {
cout << "Queue is empty\n";
} else {
cout << "Queue elements are: ";
int i = front;
while (true) {
cout << arr[i] << " ";
if (i == rear) break;
i = (i + 1) % MAX_SIZE;
}
cout << endl;
}
}
};
int main() {
CircularQueue cq;
int choice, data;
cout << "Name: Umesh Patel\n";
cout << "Enrollment No: 0126AL231140\n";
while (true) {
cout << "\n1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter data to enqueue: ";
cin >> data;
cq.enqueue(data);
break;
case 2:
data = cq.dequeue();
if (data != -1) {
cout << "Dequeued element: " << data << endl;
}
break;
case 3:
cq.display();
break;
case 4:
return 0;
default:
cout << "Invalid choice\n";
}
}
}