Double stack queue for fast acquisition of minimum queue maximum value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.