#C8835. Stack Operations Simulation
Stack Operations Simulation
Stack Operations Simulation
You are given a series of commands to simulate operations on a stack. The commands are of three types:
- push x: Push an integer x onto the stack.
- pop: Remove the top element of the stack if it exists.
- top: Print the top element of the stack; if the stack is empty, print
Empty
.
The stack follows the Last-In-First-Out (LIFO) principle. Mathematically, if elements are pushed in the order \(a_1, a_2, \dots, a_k\), then \(\text{top} = a_k\).
Your task is to process the commands and output the result for every top
command.
inputFormat
The input starts with an integer (n) ((1 \le n \le 10^5)) indicating the number of commands. The following (n) lines each contain one of the commands: push x
(where x is an integer), pop
, or top
.
outputFormat
For each top
command in the input, output a single line containing the top element of the stack. If the stack is empty when a top
command is executed, output Empty
.## sample
7
push 5
push 3
top
pop
top
pop
top
3
5
Empty
</p>