The topic describes the data structure of the definition stack, and implements a min function in that type that can get the smallest element contained in the stack.
#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<cstdlib>#include<algorithm>#include<stack>using namespacestd;classSolution { Public: voidPushintvalue) {St.push (value); if(Smin.empty ()) {Smin.push (value); } if(Smin.top () >value) {Smin.push (value); } } voidpop () {if(St.top () = =Smin.top ()) {Smin.pop (); } st.pop (); } intTop () {returnSt.top (); } intmin () {returnSmin.top (); } Private: Stack<int>St; Stack<int>smin;};intMain () {solution S; S.push (2); S.push (1); S.push (3); S.push (4); S.push (4); S.push (5); S.push (3); cout<<s.top () <<Endl; cout<<s.min () <<Endl; return 0;}
Stack with min function (Sword offer,c++)