Group
Data Structures: Stacks, Queues in C++
Objective
1. Define a fixed-size array to represent the queue storage.
2. Use two variables to keep track of the front and rear of the queue.
3. Implement a function to enqueue elements, checking for overflow.
4. Implement a function to dequeue elements, checking for underflow.
5. Allow the user to interact with the queue using a simple text menu.
Create a queue and perform insertion and deletion operations.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include input/output stream library
using namespace std;
#define MAX 5 // Define the maximum size of the queue
// Queue class definition
class Queue {
private:
int arr[MAX]; // Array to store queue elements
int front, rear; // Pointers for front and rear positions
public:
// Constructor to initialize queue
Queue() {
front = -1;
rear = -1;
}
// Function to insert an element into the queue
void enqueue(int value) {
if (rear == MAX - 1) {
cout << "Queue Overflow. Cannot enqueue " << value << endl;
} else {
if (front == -1) front = 0; // Initialize front if it's the first element
rear++; // Move rear pointer
arr[rear] = value; // Store the value at rear position
cout << "Enqueued " << value << " into the queue." << endl;
}
}
// Function to remove an element from the queue
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow. Nothing to dequeue." << endl;
} else {
cout << "Dequeued " << arr[front] << " from the queue." << endl;
front++; // Move front pointer forward
}
}
// Function to display the contents of the queue
void display() {
if (front == -1 || front > rear) {
cout << "Queue is empty." << endl;
} else {
cout << "Queue contents: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Queue q; // Create a Queue object
int choice, value;
// Loop to show menu and perform queue operations
do {
cout << "\nQueue Operations Menu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display Queue\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
q.enqueue(value); // Call enqueue function
break;
case 2:
q.dequeue(); // Call dequeue function
break;
case 3:
q.display(); // Call display function
break;
case 4:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4); // Continue until the user chooses to exit
return 0; // End of program
}
Output
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
Enter your choice: 1
Enter value to enqueue: 10
Enqueued 10 into the queue.
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
Enter your choice: 1
Enter value to enqueue: 20
Enqueued 20 into the queue.
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
Enter your choice: 3
Queue contents: 10 20
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
Enter your choice: 2
Dequeued 10 from the queue.
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
Enter your choice: 4
Exiting program.