Skip to content

Commit 507f4f3

Browse files
authored
Updated circular_queue_using_array.cpp
The current code is more efficient and more structured than the previous one, both in terms of readability and performance. I removed some unnecessary checks and added more documentation
1 parent 5eff2b9 commit 507f4f3

File tree

1 file changed

+74
-34
lines changed

1 file changed

+74
-34
lines changed
Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,97 @@
11
#include <iostream>
22
using namespace std;
33

4-
int queue[10];
5-
int front = 0;
6-
int rear = 0;
7-
int count = 0;
8-
9-
void Enque(int x) {
10-
if (count == 10) {
11-
cout << "\nOverflow";
4+
// Define the maximum size of the queue.
5+
const int MAX_SIZE = 10;
6+
7+
int queue[MAX_SIZE]; // Queue array
8+
int front = 0; // Points to the front of the queue
9+
int rear = 0; // Points to the next available position for inserting elements
10+
int count = 0; // Keeps track of the number of elements in the queue
11+
12+
// Function to enqueue an element into the queue
13+
void Enqueue(int x) {
14+
// Check if the queue is full
15+
if (count == MAX_SIZE) {
16+
cout << "\nOverflow: Queue is full, cannot enqueue " << x;
1217
} else {
18+
// Insert the element at the rear and move the rear pointer circularly
1319
queue[rear] = x;
14-
rear = (rear + 1) % 10;
15-
count++;
20+
rear = (rear + 1) % MAX_SIZE;
21+
count++; // Increase the element count
1622
}
1723
}
1824

19-
void Deque() {
20-
if (front == rear) {
21-
cout << "\nUnderflow";
22-
}
23-
24-
else {
25+
// Function to dequeue an element from the queue
26+
void Dequeue() {
27+
// Check if the queue is empty
28+
if (count == 0) {
29+
cout << "\nUnderflow: Queue is empty, cannot dequeue";
30+
} else {
31+
// Display the dequeued element and move the front pointer circularly
2532
cout << "\n" << queue[front] << " deleted";
26-
front = (front + 1) % 10;
27-
count--;
33+
front = (front + 1) % MAX_SIZE;
34+
count--; // Decrease the element count
2835
}
2936
}
3037

38+
// Function to display the elements of the queue
3139
void show() {
40+
if (count == 0) {
41+
cout << "\nQueue is empty";
42+
return;
43+
}
44+
45+
cout << "\nQueue elements: ";
46+
// Loop through the valid elements using modular arithmetic
3247
for (int i = 0; i < count; i++) {
33-
cout << queue[(i + front) % 10] << "\t";
48+
cout << queue[(front + i) % MAX_SIZE] << "\t";
3449
}
3550
}
3651

52+
// Main function with a menu-driven approach for queue operations
3753
int main() {
38-
int ch, x;
54+
int choice, x;
55+
3956
do {
40-
cout << "\n1. Enque";
41-
cout << "\n2. Deque";
42-
cout << "\n3. Print";
43-
cout << "\nEnter Your Choice : ";
44-
cin >> ch;
45-
if (ch == 1) {
46-
cout << "\nInsert : ";
47-
cin >> x;
48-
Enque(x);
49-
} else if (ch == 2) {
50-
Deque();
51-
} else if (ch == 3) {
52-
show();
57+
// Display menu
58+
cout << "\n\nMenu:";
59+
cout << "\n1. Enqueue";
60+
cout << "\n2. Dequeue";
61+
cout << "\n3. Show";
62+
cout << "\n0. Exit";
63+
cout << "\nEnter your choice: ";
64+
cin >> choice;
65+
66+
switch (choice) {
67+
case 1:
68+
// Enqueue an element
69+
cout << "\nInsert: ";
70+
cin >> x;
71+
Enqueue(x);
72+
break;
73+
74+
case 2:
75+
// Dequeue an element
76+
Dequeue();
77+
break;
78+
79+
case 3:
80+
// Show the current queue elements
81+
show();
82+
break;
83+
84+
case 0:
85+
// Exit the loop
86+
cout << "\nExiting...";
87+
break;
88+
89+
default:
90+
// Handle invalid input
91+
cout << "\nInvalid choice, please try again.";
5392
}
54-
} while (ch != 0);
93+
94+
} while (choice != 0); // Continue until the user chooses to exit
5595

5696
return 0;
5797
}

0 commit comments

Comments
 (0)