Define a queue with two stacks

Source: Internet
Author: User
Tags stack pop
A stack is used for pressing elements such
Another stack pop-up element. If an empty stack is encountered in a pop-up operation, the elements in the stack are transmitted to the stack one by one.

# Include "stdafx. H"
# Include <stack>

Template <class T, Class C = STD: vector <t>
Class queue ...{
Public:
Typedef typename C: vaule_type;
Typedef typename C: size_type;
Typedef typename C conainer_type;

Explicit Queue (C const & contents = C ())
: Outstack _ (contents )...{}
Bool empty () const
... {Return instack _. Empty () & outstack _. Empty ();}

Size_type size () const
... {Return instack _. Size () + outstack _. Size ();}

Vaule_type & Front ()
... {Transfer (); Return outstack _. Top ();}
Vaule_type const & Front ()
... {Transfer (); Return outstack _. Top ();}

Vaule_type & back ()
... {Untransfer (); Return instack _. Top ();}
Vaule_type const & back () const
... {Untransfer (); Return instack _. Top ();}

Void push (vaule_type const & X)... {instack _. Push (x );}
Void POP ()... {transfer (); outstack _. Pop ();}

PRIVATE:
Void transfer ()...{
If (outstack _. Empty ())
While (! Instack _. Empty ())...{
Outstack _. Push (instack _. Top ());
Instack _. Pop ();
}
}

Void untransfer ()...{
Assert (cerr <"Warning: queue: unteansfer () 'ed .");
If (instack _. Empty ())
While (! Outstack _. Empty ())...{
Instack _. Push (outstack_top ());
Outstack _. Pop ();
}
}
STD: Stack <T, C> instack _, outstack _;
T * outbottom _;
};




Int main (INT argc, char * argv [])
...{
Return 0;
}

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.