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

Задача 38601. Stack with error protection


Задача

Темы: Стек
Implement a "stack" data structure. Write a program that describes the stack and simulates how the stack works by implementing all the methods listed 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 print 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 print 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 there is a back or pop operation 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

Imprint
The program should output the stack log, one message per line
 
Examples
# Input Output
1 size
push 1

push 2

push 3

exit
0
ok
1
ok
2
ok
3
bye

Запрещенные операторы:stack;queue;deque