|
1 | 1 | #include <iostream>
|
2 | 2 | using namespace std;
|
3 | 3 |
|
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; |
12 | 17 | } else {
|
| 18 | + // Insert the element at the rear and move the rear pointer circularly |
13 | 19 | queue[rear] = x;
|
14 |
| - rear = (rear + 1) % 10; |
15 |
| - count++; |
| 20 | + rear = (rear + 1) % MAX_SIZE; |
| 21 | + count++; // Increase the element count |
16 | 22 | }
|
17 | 23 | }
|
18 | 24 |
|
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 |
25 | 32 | cout << "\n" << queue[front] << " deleted";
|
26 |
| - front = (front + 1) % 10; |
27 |
| - count--; |
| 33 | + front = (front + 1) % MAX_SIZE; |
| 34 | + count--; // Decrease the element count |
28 | 35 | }
|
29 | 36 | }
|
30 | 37 |
|
| 38 | +// Function to display the elements of the queue |
31 | 39 | 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 |
32 | 47 | for (int i = 0; i < count; i++) {
|
33 |
| - cout << queue[(i + front) % 10] << "\t"; |
| 48 | + cout << queue[(front + i) % MAX_SIZE] << "\t"; |
34 | 49 | }
|
35 | 50 | }
|
36 | 51 |
|
| 52 | +// Main function with a menu-driven approach for queue operations |
37 | 53 | int main() {
|
38 |
| - int ch, x; |
| 54 | + int choice, x; |
| 55 | + |
39 | 56 | 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."; |
53 | 92 | }
|
54 |
| - } while (ch != 0); |
| 93 | + |
| 94 | + } while (choice != 0); // Continue until the user chooses to exit |
55 | 95 |
|
56 | 96 | return 0;
|
57 | 97 | }
|
0 commit comments