-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp7OperationOnCircularQueue.cpp
More file actions
132 lines (123 loc) · 2.97 KB
/
p7OperationOnCircularQueue.cpp
File metadata and controls
132 lines (123 loc) · 2.97 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Design, Develop and Implement a menu driven Program for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX) a. Insert an Element on to Circular QUEUE b. Delete an Element from Circular QUEUE c. Demonstrate Overflow and Underflow situations on Circular QUEUE d. Display the status of Circular QUEUE
#include <iostream>
#define MAX 5
using namespace std;
class CircularQueue
{
private:
char queue[MAX];
int front, rear;
public:
CircularQueue()
{
front = rear = -1;
}
bool isFull()
{
return (front == 0 && rear == MAX - 1) || (front == rear + 1);
}
bool isEmpty()
{
return front == -1;
}
void insert(char element)
{
if (isFull())
{
cout << "Queue Overflow! Cannot insert." << endl;
return;
}
if (front == -1)
front = 0;
rear = (rear + 1) % MAX;
queue[rear] = element;
cout << "Inserted: " << element << endl;
}
void remove()
{
if (isEmpty())
{
cout << "Queue Underflow! Cannot delete." << endl;
return;
}
cout << "Deleted: " << queue[front] << endl;
if (front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % MAX;
}
}
void display()
{
if (isEmpty())
{
cout << "Queue is Empty!" << endl;
return;
}
cout << "Circular Queue: ";
int i = front;
while (true)
{
cout << queue[i] << " ";
if (i == rear)
break;
i = (i + 1) % MAX;
}
cout << endl;
}
void checkStatus()
{
if (isFull())
{
cout << "\nQueue Overflow Condition:\n";
display();
}
else if (isEmpty())
{
cout << "\nQueue Underflow Condition:\nQueue is empty, no elements to display.\n";
}
else
{
cout << "Queue is neither full nor empty." << endl;
display();
}
}
};
int main()
{
cout << "ABHISHEK SINGH 2315272/2435222";
CircularQueue cq;
int choice;
char element;
do
{
cout << "\n1. Insert\n2. Delete\n3. Display\n4. Check Queue Status(overflow/underflow)\n5. Exit\nEnter choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter character to insert: ";
cin >> element;
cq.insert(element);
break;
case 2:
cq.remove();
break;
case 3:
cq.display();
break;
case 4:
cq.checkStatus();
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 5);
return 0;
}