Stack 2
InterviewTime limit2sMemory limit1024 MB
Process N stack commands that push integers, pop or peek the top value, report the size, and check emptiness, printing -1 when the stack is empty.
- Level
Easy2 of 10
- Topics
- Stack, Implementation
- Solved
- No attempts yet
Problem
Implement a stack that stores integers, then write a program that processes the commands given as input. There are five commands.
1 X: Push the integerXonto the stack. (1 ≤X≤ 100,000)2: If the stack has an integer, remove the top integer and print it. Otherwise, print-1.3: Print the number of integers in the stack.4: Print1if the stack is empty, otherwise print0.5: If the stack has an integer, print the top integer. Otherwise, print-1.
Input
The first line contains the number of commands N (1 ≤ N ≤ 1,000,000). The next N lines each contain one command. At least one command asks for output.
Output
Each time a command that requires output is given, print its result on its own line.