#K84707. Implement Stack with Push and Pop Operations
Implement Stack with Push and Pop Operations
Implement Stack with Push and Pop Operations
The task is to implement a stack that supports two main operations: push and pop. The stack should work as follows: when an element is pushed onto the stack, it is added to the top; when a pop operation is performed, the element at the top of the stack is removed and returned. If the stack is empty, the pop operation should return (-1). You must implement the stack using a linked list structure.
Input will consist of a series of operations. Each operation is either to push an integer onto the stack or to pop the top element. Your program should process the operations in order, reading from standard input and writing the results of every pop to standard output.
inputFormat
The first line of input contains an integer (N), which represents the number of operations. The next (N) lines each contain an operation in one of the following formats:
• push x
— Push the integer (x) onto the stack.
• pop
— Pop the top element from the stack and print its value.
Operations are processed sequentially.
outputFormat
For every pop operation, output the returned value on a new line. If the stack is empty when a pop operation is requested, output (-1).## sample
5
push 2
push 3
pop
push 4
pop
3
4
</p>