What the following code does is to try and store the minimum value which would be added to the base stack inside another stack which in my example is called s2
(and when that min value would be popped,it would also be popped from the Stack s2
), what I don't understand is how does base.Push(value);
in the push
method and base.Pop();
in the pop
method work?
My super class is Stack<int>
which means I can use its methods including push
and pop
but in my example if I use my push
method on an Integer
which will call the base.Push
on that Integer
, but where does that base.Push
method adds that Integer
since the only stack that I created and initialized is the Stack s2
class StackWithMin2 : Stack<int> {
Stack<int> s2;
public StackWithMin2() {
s2 = new Stack<int>();
}
public void push(int value) {
if (value<=min()) {
s2.Push(value);
}
base.Push(value);
}
public int pop() {
int value = base.Pop();
if (value==min() {
s2.Pop();
}
return value;
}
public int min() {
if (s2.Count == 0)
{
return int.MaxValue;
}
else {
return s2.Peek();
}
}
}