PP2: Week 9,10: May 17 - 30
Stack with error protection
Implement the "stack" data structure. Write a program containing the description and the modeling of the stack, implementing all the methods specified below. The program reads a sequence of commands and, depending on the command, performs one or another operation. After each command is executed, the program should output one line. Possible commands for the program:
- push n - Add number n into the stack (n is non-negative). Program must print ok.
- pop - Remove from the stack the last element. The program must print its value.
- back - The program must print the value of the last element without removing it out of the stack.
- size - The program must print Программа должна вывести количество элементов в стеке.
- clear - The program must clean the stack and print ok.
- exit - The program must print bye and terminate.
Before executing operations back and pop the program must check whether the stack contains at least one element. If the input data contains operation back or pop, but the stack is empty, instead of the number print the string error.
Each line contains one command.
For each command print in separate line the corresponding result.
push 2 back pop size pop push 1 size exit
ok 2 2 0 error ok 1 bye