Implement Stack Using Arrays
Implement a stack data structure using a fixed-size array.
Input format
The first line contains Q — the number of operations. Each of the next Q lines is one of: push x, pop, top, size.
Output format
For each pop print the removed value, for each top print the top value, for each size print the element count — one result per line. pop/top on an empty stack print -1.
Constraints
- Values fit in a 64-bit signed integer
- Trailing whitespace and a trailing newline are ignored by the judge
Read from stdin, write to stdout. Sample cases below show the exact format.
Sample cases
Example 1
Input
9
push 61
size
push 57
pop
pop
pop
size
pop
sizeExpected output
1
57
61
-1
0
-1
0Example 2
Input
8
push 8
top
push 40
push 83
top
pop
push 5
sizeExpected output
8
83
83
3