1 Ideas:
Implement a stack yourself, where members are stacks in the standard library, one holds all the elements, one holds the smallest element, and one holds the largest element.
Use your own implementation of the stack to achieve a maximum minimum value of the queue, which contains two members, one as the stack of the team, one as the queued stack.
2 C + + implementation code:
#include <iostream>#include<stack>#include<cstdlib>using namespacestd;template<typename t>classminmaxstack{ Public: BOOLempty () {returnSt.empty (); } size_t Size () {returnst.size (); } voidPushintx) {if(Minstack.empty () | | x<minstack.top ()) Minstack.push (x); if(Maxstack.empty () | | X>maxstack.top ()) Maxstack.push (x); St.push (x); } voidpop () {if(St.empty ())return; if(St.top () = =minstack.top ()) Minstack.pop (); if(St.top () = =maxstack.top ()) Maxstack.pop (); St.pop (); } intgetmin () {if(St.empty ())return-1; returnMinstack.top (); } intGetmax () {if(St.empty ())return-1; returnMaxstack.top (); } intTop () {returnSt.top (); }Private: Stack<int>St; Stack<int>Minstack; Stack<int>maxstack;}; Template<typename t>classmyqueue{ Public: BOOLempty () {return inch. Empty () && out. empty (); } size_t Size () {return inch. Size () + out. Size (); } intGetmax () {if(inch. Empty () && out. Empty ())return-1; if(inch. Empty ())return out. Getmax (); if( out. Empty ())return inch. Getmax (); returnMaxinch. Getmax (), out. Getmax ()); } intgetmin () {if(inch. Empty () && out. Empty ())return-1; if(inch. Empty ())return out. Getmin (); if( out. Empty ())return inch. Getmin (); returnMininch. Getmin (), out. Getmin ()); } voidPushintx) {inch. push (x); } voidpop () {if(inch. Empty () && out. Empty ())return; if( out. Empty ()) { while(!inch. Empty ()) { out. Push (inch. Top ()); inch. Pop (); } } out. Pop (); }Private: Minmaxstack<int>inch; Minmaxstack<int> out;};intMain () {Myqueue<int>Q; for(inti =0; I < the; i++) { intIndex=rand ()% -; cout<<index<<' '; Q.push (index); } cout<<q.getmax () <<Endl; cout<<q.getmin () <<Endl;}
Double stack queue for fast acquisition of minimum queue maximum value