Simulate a Print Queue Using a Queue Data Structure in C++

In this C++ exercise, you will simulate a print queue where multiple print jobs are processed in the order they are received. The program will use a queue data structure to store the print jobs and process them in a first-in, first-out (FIFO) manner. Each print job will be handled one by one, and once a job is completed, it will be removed from the queue. The program will allow adding new print jobs and processing the existing ones.

Group

Data Structures: Stacks, Queues in C++

Objective

1. Create a queue to store print jobs.
2. Add new print jobs to the queue.
3. Process the print jobs by removing them from the queue in the order they were added.
4. Output the status of the print jobs (processed or pending) after each operation.

Write a program that simulates a print queue.

Example C++ Exercise

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

using namespace std;

// Function to simulate the print queue
void processPrintQueue(queue<string>& printQueue) {
    // Check if the queue is empty
    while (!printQueue.empty()) {
        // Process the next print job in the queue
        cout << "Processing print job: " << printQueue.front() << endl;
        printQueue.pop();  // Remove the processed job from the queue
    }
    cout << "All print jobs have been processed." << endl;
}

// Main function
int main() {
    queue<string> printQueue; // Queue to store the print jobs

    // Add print jobs to the queue
    printQueue.push("Document1.pdf");
    printQueue.push("Image1.png");
    printQueue.push("Report.docx");

    cout << "Initial print queue: " << endl;
    cout << "1. Document1.pdf" << endl;
    cout << "2. Image1.png" << endl;
    cout << "3. Report.docx" << endl;

    // Process the print jobs
    processPrintQueue(printQueue);  // Call the function to process the jobs

    return 0;
}

 Output

Initial print queue:
1. Document1.pdf
2. Image1.png
3. Report.docx
Processing print job: Document1.pdf
Processing print job: Image1.png
Processing print job: Report.docx
All print jobs have been processed.

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