Data Structures · Queue

Question 2: Queue Application — Clinic Waiting Line Simulator

⌨️ The command being read (cin)

>(waiting for input)
The program reads commands one at a time with while (cin >> cmd), handling each as it arrives.

🚶 The waiting queue queue<string>

← front (the next patient to be called)rear (new arrivals line up here) →
front (FIFO: first in, first seen) just arrived (ARRIVE) being peeked at (SHOW) called in (CALL)

🖥️ Console output (cout)

💬 Current step

💻 The C++ program (std::queue<string>)

Yellow = current line. ARRIVE = push (enqueue); CALL = read front() then pop() (dequeue); SHOW = read front() only — the queue is untouched.

📄 The question’s wording & key points

Problem Description: simulate a simple clinic waiting system with std::queue<std::string>. The program processes three kinds of actions:
1. ARRIVE <name> — a patient enters the queue (enqueue)
2. CALL — the doctor calls the patient at the front (dequeue) and prints their name; if the queue is empty, print "No patients waiting"
3. SHOW — print the name of the patient currently next in line without removing them (front / peek)
1ARRIVE = enqueue (push) — the new patient joins the rear
2CALL = dequeue (pop) — always the front; an empty queue must be reported
3SHOW = peek, never dequeue — the queue is completely unchanged
CommandQueue operationOutput
ARRIVE Alicepush(Alice) at the rearAlice joined the queue
CALLfront() then pop()Now serving: Alice
CALL (empty)does nothingNo patients waiting
SHOWreads front() onlyNext in line: Bob
Why a queue? A clinic is first come, first served — that is exactly FIFO: enqueue at the rear, dequeue at the front. With a stack (LIFO) the last patient to arrive would be called first, which would scramble the order.
Complexity: push / pop / front are all O(1), so the whole simulation is O(number of commands).
Note: the slide’s "Example Actions" list is cut off in the photo, so the three sequences here are mine — each one covers all three actions and the empty-queue message.
Speed
Shortcuts: Space play/pause · ←→ step · R reset · E result