#K1511. Stack Operations Using Linked List

    ID: 24284 Type: Default 1000ms 256MiB

Stack Operations Using Linked List

Stack Operations Using Linked List

You are given a series of operations to perform on a stack. The operations are provided as strings and are of two types:

  • push x: Push an integer x onto the stack.
  • pop: Remove and return the top element from the stack. If the stack is empty, return -1.

Your task is to implement the stack using a linked list. Each push operation should add a new node at the beginning of the list, while each pop operation should remove the node at the beginning of the list and output its value.

The input starts with the number of operations, followed by each operation on a new line. For every pop operation, output the result on a separate line.

All formulas are in \(\LaTeX\) format if needed. In this problem, the operations follow the Last In First Out (\(\text{LIFO}\)) principle: if you push elements \(a\) then \(b\), a subsequent pop returns \(b\) (i.e. the most recently added element).

inputFormat

The first line of input contains an integer (n) representing the number of operations. Each of the next (n) lines contains an operation in one of the following formats: "push x" (where (x) is an integer) or "pop".

outputFormat

For each "pop" operation, output the result on a new line. If the stack is empty when a pop operation is executed, output -1.## sample

6
push 3
push 5
pop
push 7
pop
pop
5

7 3

</p>