Activity 33: Printer Queue Simulator

Goal: Implement a printer job queue using VecDeque with methods to manage print jobs. (You can use Rust Playground - just copy the template code.)

Part 1: Setup

use std::collections::VecDeque;

struct PrintJob {
    id: u32,
    pages: u32,
}

struct PrinterQueue {
    jobs: VecDeque<PrintJob>,
}

impl PrinterQueue {
    fn new() -> Self {
        // TODO: Create a new empty PrinterQueue
    }

    fn add_job(&mut self, id: u32, pages: u32) {
        // TODO: Add a normal job to the back of the queue
    }

    fn add_urgent_job(&mut self, id: u32, pages: u32) {
        // TODO: Add an urgent job to the front of the queue
    }

    fn total_pages(&self) -> u32 {
        // TODO: Calculate and return the total pages of all jobs in the queue
    }

    fn print_next(&mut self) {
        // TODO: Remove the next job from the queue and print:
        // "Processing job [id]: [pages] pages"
        // If the queue is empty, print: "No jobs in queue"
    }

    fn print_all(&mut self) {
        // TODO: Process and print all remaining jobs until the queue is empty
    }
}

fn main() {
    let mut printer = PrinterQueue::new();

    // Add some regular jobs
    printer.add_job(1, 5);
    printer.add_job(2, 10);
    printer.add_job(3, 3);

    println!("Total pages in queue: {}", printer.total_pages());

    // Process one job
    printer.print_next();

    // Add an urgent job (should go to front)
    printer.add_urgent_job(99, 2);

    // Process all remaining jobs
    printer.print_all();
}

Expected Output

Total pages in queue: 18
Processing job 1: 5 pages
Processing job 99: 2 pages
Processing job 2: 10 pages
Processing job 3: 3 pages

Part 2: Extensions (if time permits)

Once you have the basic functionality working, try adding:

  1. fn queue_size(&self) -> usize - Returns the number of jobs in the queue

  2. fn peek_next(&self) -> Option<&PrintJob> - Look at the next job without removing it

  3. fn cancel_job(&mut self, id: u32) -> bool - Remove a specific job by ID, return true if found

Discussion Questions

  1. Why is VecDeque better than Vec for this problem?
  2. What would happen if we used Vec and had many urgent jobs?
  3. In what scenarios would add_urgent_job be useful in real systems?
  4. What's the time complexity of total_pages()? How could we make it O(1)?