Simulate a Bank Queue using a Queue Data Structure in C++

In this C++ exercise, you will implement a program to simulate a bank queue system using a queue data structure. The program will handle customers in the queue, allowing them to be served in the order they arrive (FIFO - First In, First Out). You will simulate adding customers to the queue, serving them, and displaying the status of the queue. The task aims to teach you how to work with queues in C++, focusing on basic queue operations such as enqueue, dequeue, and checking if the queue is empty.

Group

Data Structures: Stacks, Queues in C++

Objective

1. Implement a queue to represent the bank queue system.
2. Add customers to the queue as they arrive.
3. Serve customers in the order they entered the queue (FIFO).
4. Display the status of the queue after each operation.
5. Ensure the program can handle basic operations like checking if the queue is empty, adding customers, and serving them.

Implement a program to simulate a bank queue using a queue data structure.

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include for input/output operations
#include <queue>      // Include for queue operations

using namespace std;

// Function to simulate the bank queue
void simulateBankQueue() {
    queue<string> bankQueue;  // Declare a queue to store customer names

    // Simulate adding customers to the queue
    bankQueue.push("John");  // Customer John arrives
    bankQueue.push("Jane");  // Customer Jane arrives
    bankQueue.push("Alice"); // Customer Alice arrives

    cout << "Initial Queue: " << endl;
    
    // Display the customers in the queue
    cout << "Customers in the queue: " << endl;
    while (!bankQueue.empty()) {
        cout << bankQueue.front() << endl;  // Display the customer at the front of the queue
        bankQueue.pop();  // Serve the customer by removing them from the queue
    }

    cout << "The queue is now empty." << endl;
}

// Main function to run the program
int main() {
    simulateBankQueue();  // Call the function to simulate the bank queue
    return 0;
}

 Output

Initial Queue: 
Customers in the queue: 
John
Jane
Alice
The queue is now empty.

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