Problems
Class MinStack
Class MinStack
Implement a stack data structure that supports next operations:
- push n - Add to the stack the number n.
- pop - Remove the last element from the stack. Print the value of removed element.
- top - Print the value of the top element, not removing it from the stack.
- GetMin - Print the value of the minimum element in the stack.
- size - Print the number of elements in the stack.
Write the code according to the next interface:
class MyStack // C++
{
private:
stack<int> s, mn; // s - normal stack, mn - minimum stack
public:
void push(int x); // Push x into the stack
int pop(void); // Remove and return the element from the top of the stack
int top(void); // Return the element from the top of the stack
int GetMin(void); // Return the minimum element from the stack
int GetSize(void); // Return the size of the stack
};
class MyStack // Java
{
private stack<Integer> s, mn; // s - normal stack, mn - minimum stack
MyStack() // Constructor
public void push(int x); // Push x into the stack
public int pop(void); // Remove and return the element from the top of the stack
public int top(void); // Return the element from the top of the stack
public int GetMin(void); // Return the minimum element from the stack
public int GetSize(void); // Return the size of the stack
};
Input
Each line contains a single command.
Output
For each command print on a separate line the corresponding result.
Input example #1
push 6 push 3 push 12 GetMin pop size GetMin
Output example #1
3 12 2 3