#C12768. Stack Implementation

    ID: 42231 Type: Default 1000ms 256MiB

Stack Implementation

Stack Implementation

This problem requires you to implement a stack supporting the following operations:

push X: Adds an integer X to the top of the stack.
pop: Removes and returns the top element of the stack; if the stack is empty, returns None.
peek: Returns the top element without removing it; if the stack is empty, returns None.
is_empty: Returns True if the stack is empty and False otherwise.
size: Returns the number of elements currently in the stack.

Note that the stack follows the Last-In-First-Out (LIFO) principle. In this problem, you must process a sequence of commands through standard input and print the relevant output for commands that require it. In case of an invalid pop or peek operation (when the stack is empty), output the string None.

inputFormat

The input starts with an integer (n) ((1 \leq n \leq 10^4)), representing the number of operations to execute. Each of the next (n) lines contains one command from the following list:

push X — push an integer (X) ((X) is a valid integer) onto the stack.
pop — remove the top element from the stack and output it, or output (None) if the stack is empty.
peek — output the top element of the stack without removing it, or output (None) if the stack is empty.
is_empty — output True if the stack is empty; otherwise, output False.
size — output the number of elements in the stack.

outputFormat

For each operation that produces an output (i.e. pop, peek, is_empty, and size), print the result on a new line. The push command does not produce any output.## sample

5
push 1
push 2
size
pop
peek
2

2 1

</p>