Implement a queue with two stacks, implement a stack with two queues, C + + STL version

Source: Internet
Author: User

No need to explain, the code here should be able to explain the problem

#include <iostream> #include <stack> #include <queue>using namespace std;//queuetemplate<    TypeName T>class queue{public:void pop (void);    void push (const t& T);    Const t& Front (void);    bool Empty (void) const;private:void left_to_rigth (void); Stack<t> Left_,right_;}; Template<typename t>void queue<t>::left_to_rigth (void) {while (!left_.empty ()) {Right_.push (Left_        . Top ());    Left_.pop (); }}template<typename t>bool queue<t>::empty (void) const{return (Left_.empty () && right_.empty ());} Template<typename t>void queue<t>::p ush (const t& t) {Left_.push (t);}    Template<typename t>void Queue<t>::p op (void) {if (!right_.empty ()) {Right_.pop ();    } else if (!left_.empty ()) {left_to_rigth ();    } else {throw "Queue is empty now, can not pop!"; }}template<typename t>const t& queue<t>::front (void) {if (!riGht_.empty ()) {return right_.top ();        } else if (!left_.empty ()) {left_to_rigth ();    return Right_.top ();    } else {throw "empty queu can not return front";    }}//stacktemplate<typename t>class stack{public:void pop (void);    void push (const t& T);    Const t& Top (void);    bool Empty (void);p rivate:void left_to_rigth (void); Queue<t> Left_,right_;}; Template<typename t>void stack<t>::left_to_rigth (void) {if (!left_.empty ()) {int move = Left_.si        Ze ()-1;            while (Move > 0) {right_.push (Left_.front ());            Left_.pop ();        --move; }}}template<typename T>bool Stack<t>::empty () {return (Left_.empty () && right_.empty ());} Template<typename t>void stack<t>::p ush (const t& t) {Left_.push (t);}    Template<typename t>void Stack<t>::p op (void) {left_to_rigth ();    Left_.swap (Right_); Right_.poP ();}        Template<typename t>const t& stack<t>::top (void) {if (!left_.empty ()) {left_to_rigth ();        Left_.swap (Right_);        Const t& TEMP = Right_.front ();        Left_.push (Right_.front ());        Right_.pop ();    return temp;    } else {throw "empty stack can not return top!";    }}int Main () {queue<int> queue_int;    cout<< "test queue:";    for (int i=0;i<10;++i) {Queue_int.push (i);        } while (!queue_int.empty ()) {Cout<<queue_int.front () << "";    Queue_int.pop ();    } cout<<endl;    Stack<int> Stack_int;    cout<< "Test stack:";    for (int i=0;i<10;++i) {Stack_int.push (i);        } while (!stack_int.empty ()) {cout<<stack_int.top () << "";    Stack_int.pop ();    } cout<<endl; return 0;}


Implement a queue with two stacks, implement a stack with two queues, C + + STL version

Related Article

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.