It is just a demonstration implementation, regardless of whether the data structure used by the stack is vector or other containers.
The Code is as follows:
# Include <iostream> # include <vector> using namespace STD; Template <class T> class minmaxstack {public: minmaxstack () {datastack = new vector <t>; minstack = new vector <int>; maxstack = new vector <int> ;}~ Minmaxstack () {Delete datastack; Delete minstack; Delete maxstack;} bool isempty () {return datastack-> size () = 0;} void push (t data) {If (isempty () {datastack-> push_back (data); minstack-> push_back (0); maxstack-> push_back (0 );} else {datastack-> push_back (data); int minindex = * (minstack-> end ()-1); int maxindex = * (maxstack-> end ()-1 ); int min = * (datastack-> begin () + minindex); int max = * (datastack-> begin () + maxindex); If (min> data) {minstack-> push_back (datastack-> size ()-1);} else {minstack-> push_back (minindex);} If (max> data) {maxstack-> push_back (maxindex);} else {maxstack-> push_back (datastack-> size ()-1) ;}} t gettop () {return * (datastack-> end ()-1);} void POP () {If (isempty () {return ;} minstack-> erase (minstack-> end ()-1); maxstack-> erase (maxstack-> end ()-1 ); datastack-> erase (datastack-> end ()-1);} t max () {return * (datastack-> begin () + (* (maxstack-> end () -1);} T min () {return * (datastack-> begin () + (* (minstack-> end ()-1 )));} void printstack () {cout <"Stack top:" <gettop () <Endl; cout <"minimum:" <min () <Endl; cout <"MAX:" <max () <Endl; cout <"\ n";} PRIVATE: vector <t> * datastack; vector <int> * minstack; vector <int> * maxstack;}; void main () {minmaxstack <int> st; ST. push (6); ST. printstack (); ST. push (17); ST. printstack (); ST. push (7); ST. printstack (); ST. push (3); ST. printstack (); ST. pop (); ST. printstack (); ST. pop (); ST. printstack (); ST. pop (); ST. printstack (); cin. get ();}
Stack with O (1) time complexity and min and Max Functions