← Back to problems
UNSW COMP2521 · medium

Queue: Simulate Dequeue Order

Queue: process a sequence of enqueue/dequeue ops

Read m operations, each E v (enqueue v) or D (dequeue). Process them with a queue and print the value dequeued for each D; if the queue is empty on a D, print -1. All outputs on one line, space-separated.

Complete enqueue and dequeue (first-in-first-out).

Pitfalls (straight from the bug lab): (1) check the queue is non-empty (count > 0) before dequeue; if empty return -1 — do not read garbage or let count go negative. (2) head advances on dequeue, preserving FIFO.

Input

  • Line 1: number of ops m.
  • Next m lines: E v (enqueue integer v) or D (dequeue).

Output

  • One line: the value for each D (or -1 if empty), space-separated, a space after each. If there are no D ops, print just a newline.

Example: input 6 / E 1 / E 2 / D / E 3 / D / D -> 1 2 3 (FIFO: out come 1, 2, 3).