Basic Queue Implementation with Enqueue and Dequeue Operations in C++

In this C++ exercise, you will create a simple queue data structure using an array. A queue is a linear structure that follows the First In, First Out (FIFO) principle. This means that the first element inserted into the queue is the first one to be removed.

The program allows the user to:
1. **Enqueue** - Insert an element at the rear of the queue.
2. **Dequeue** - Remove an element from the front of the queue.
3. **Display** - Show the current contents of the queue.

This exercise is excellent for learning the basics of queues and understanding how to manage front and rear pointers while handling overflow and underflow conditions.

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

 Copy 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.

Share this C++ Exercise


More C++ Programming Exercises of Data Structures: Stacks, Queues in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.