This article has referred to the famous online post, Microsoft and other companies, data structure + algorithm interview 100 series
// Write c ++ <br/> // Add an auxiliary stack with the current minimum value at the top of the stack. When the stack is imported, compare the size of the data on the stack and the elements on the top of the stack, if it is <= the top element of the secondary stack, the data is added to the secondary stack after the data is imported into the stack. Because of this, the top element of the primary stack is always greater than or equal to the top element of the secondary stack. When the primary stack is out, the ratio of the top element of the primary/secondary stack to that of the secondary stack is higher, when they are equal, they all go out of the stack, or only the primary stack goes out of the stack. </P> <p> // one of the problems that bothers me is how to return error information when the stack is empty and then pop? How do I know whether it is an error message or an out-of-stack element? </P> <p> # include <iostream> <br/> using namespace STD; </P> <p> # define maxlength 10 </P> <p> class minstack <br/> {<br/> int * pstackarray; // master stack <br/> int * pminstack; // secondary stack <br/> int stop, MTop; // master stack top and secondary stack top </P> <p> // constructor, initialization data <br/> Public: minstack () <br/>{< br/> pstackarray = new int [maxlength]; <br/> pminstack = new int [maxlength]; <br/> stop = MTop =-1; <br/>}< br/> // forget to write the destructor. If you want to read an article, write it yourself. </P> <p> bool isempty () {</P> <p> If (-1 = stop) <br/> return true; <br/> else return false; <br/>}< br/> bool isfull () {</P> <p> If (maxlength-1 = stop) <br/> return true; <br/> else return false; <br/>}< br/> void push (int n) {</P> <p> If (! Isfull () {</P> <p> pstackarray [++ stop] = N; <br/> If (isempty () pminstack [++ MTop] = N; <br/> else if (n <= pminstack [MTop]) pminstack [++ MTop] = N; <br/>}< br/> int POP () {</P> <p> If (! Isempty () {</P> <p> If (pstackarray [Stop] = pminstack [MTop]) MTop --; </P> <p> stop --; <br/> return pstackarray [Stop + 1]; <br/>}< br/> int gettop () {</P> <p> If (! Isempty () return pstackarray [Stop]; <br/>}< br/> int getmin () {</P> <p> If (! Isempty () return pminstack [MTop]; <br/>}< br/>}; </P> <p> // The main element is self-written, I can write a little bit about it. I 'd like to test whether there is any problem with this class. I 'd like to say <br/>. </P> <p> int main () <br/> {<br/> minstack; <br/> minstack. push (1); <br/> minstack. push (2); <br/> minstack. push (0); <br/> minstack. push (3); </P> <p> int I = 4; <br/> while (I> 0) {</P> <p> cout <minstack. pop () <"; <br/> cout <minstack. getmin () <Endl; <br/> I --; <br/>}< br/> return 0; <br/>}