Олимпиадный тренинг

Задача 34969. Error-Proof Stack Structure Implementation


Задача

Темы: Стек
Implement a "stack" data structure. Write a program containing a description of the stack and simulating the operation of the stack, implementing all the methods indicated here. The program reads a sequence of commands and, depending on the command, performs one or another operation. After executing each command, the program should output one line. Possible commands for the program:

push n
Push the number n onto the stack (the value of n is given after the command). The program should output ok.

pop
Remove the last element from the stack. The program should output its value.

back
The program should output the value of the last element without removing it from the stack.

size
The program should print the number of elements on the stack.

clear
The program should clear the stack and print ok.

exit
The program should print bye and exit.

Before executing the back and pop operations, the program must check whether the stack contains at least one element. If the  back or pop operation occurs in the input data, and the stack is empty, then the program should output the string error instead of a numeric value.

Input: Stack management commands are entered, one per line

Output: The program should print the stack log, one message per line

Examples
input
size
push 1
size
push 2
size
push 3
size
exit
output
0
ok
1
ok
2
ok
3
bye