#C14016. Stack Implementation Challenge
Stack Implementation Challenge
Stack Implementation Challenge
You are required to implement a stack data structure that supports the following operations:
- push x: Push the integer x onto the stack.
- pop: Remove the top element of the stack and print it. If the stack is empty, print
error
. - top: Print the top element of the stack without removing it. If the stack is empty, print
error
. - isEmpty: Print
true
if the stack is empty; otherwise, printfalse
.
The operations are given as commands from stdin
. Your stack should behave according to the Last-In-First-Out (LIFO) principle. Specifically, if we denote the stack as \( S = \{a_1, a_2, \ldots, a_n\} \) where \( a_n \) is the top element, then:
Implement the stack and process a series of commands accordingly.
inputFormat
The first line of input contains an integer n
(1 ≤ n ≤ 105), representing the number of operations.
Each of the following n
lines contains one of the following commands:
push x
wherex
is an integer (\(-10^9 \le x \le 10^9\)).pop
top
isEmpty
Note: For push
commands, no output is expected.
outputFormat
For each command that produces an output (pop
, top
, isEmpty
), print the result on a separate line. The outputs are defined as follows:
- For
pop
andtop
: print the integer value if the stack is non-empty. If the stack is empty, printerror
. - For
isEmpty
: printtrue
if the stack is empty, otherwise printfalse
.
5
push 10
push 20
top
pop
top
20
20
10
</p>