#C2476. Stack and Queue Simulation

    ID: 45796 Type: Default 1000ms 256MiB

Stack and Queue Simulation

Stack and Queue Simulation

In this problem, you are given a series of commands that operate on a stack and a queue. You need to simulate these operations and output the final states of the two data structures.

The commands are given in the following forms:

  1. STACK PUSH x: Push the integer x onto the stack.
  2. STACK POP: Pop the top element from the stack (if the stack is not empty).
  3. QUEUE ENQUEUE x: Enqueue the integer x into the queue.
  4. QUEUE DEQUEUE: Dequeue the first element from the queue (if the queue is not empty).

After processing all commands, output the final state of the stack and the queue.

For the stack, print the elements from top to bottom separated by a space. If the stack is empty, output EMPTY. For the queue, print the elements from front to rear separated by a space. If the queue is empty, output EMPTY.

All input should be read from standard input and all output should be written to standard output.

inputFormat

The first line contains an integer n, the number of commands. The next n lines each contain one command, which can be one of the following:

  • STACK PUSH x — Push the integer x onto the stack.
  • STACK POP — Pop the top of the stack (if it is not empty).
  • QUEUE ENQUEUE x — Enqueue the integer x into the queue.
  • QUEUE DEQUEUE — Dequeue the front element of the queue (if it is not empty).

outputFormat

Output two lines:

  1. The first line contains the final state of the stack. If the stack is empty, print EMPTY. Otherwise, print the elements from the top to the bottom separated by a single space.
  2. The second line contains the final state of the queue. If the queue is empty, print EMPTY. Otherwise, print the elements from the front to the rear separated by a single space.## sample
6
STACK PUSH 10
QUEUE ENQUEUE 15
STACK PUSH 20
STACK POP
QUEUE DEQUEUE
QUEUE ENQUEUE 5
10

5

</p>