How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.
1 template2 class StackWithMin 3 { 4 public: 5 stack() 6 { 7 stackTop = -1; 8 minStackItemIndex = -1; 9 }10 void Push(T x)11 { stackTop++;12 if(stackTop>MAXN)13 //超出栈的最大存储量;14 else 15 {16 stackItem[stackTop] = x;17 if(x =0)44 return stackItem[minStackItemIndex];45 else46 return INT_MAX;47 }48 49 private:50 T stackItem[MAXN];51 int stackTop;52 int NextMinItem[MAXN];53 int minStackItemIndex;54 55 };