Use stacks to implement queues-use queues to implement stacks

Source: Internet
Author: User

Stack features: FILO (First In Last Out) can only insert and delete elements from the top of the stack. The most basic interfaces include push ()-push elements from the top of the stack, pop ()-pop element queue features from the top of the stack: First In First Out) only elements can be deleted from the team header and inserted from the team end. The most basic interfaces include enque () -- Insert elements from the end of the team, deque () -- delete element 1 from the team header. use two stacks to implement the queue idea: the elements in the new queue are pushed into stack1. When the elements exit the queue, if stack2 is empty, all the elements of stack1 are popped up in sequence and pushed into stack2, in this way, the top element of stack2 is the Header element. [Cpp] template <typename T> class MyQueue {public: T front (); T back (); void enque (const T & ele); void deque (); private: void move (std: stack <T> & from, std: stack <T> & to); private: std: stack <T> stack1; std :: stack <T> stack2 ;}; template <typename T> void MyQueue <T >:: move (std: stack <T> & from, std :: stack <T> & to) {if (. empty () {while (! From. empty () {. push (from. top (); from. pop () ;}}template <typename T> T MyQueue <T >:: front () {T ele; move (stack1, stack2); if (! Stack2.empty () {ele = stack2.top ();} return ele;} template <typename T> T MyQueue <T >:: back () {T ele; move (stack2, stack1); if (! Stack1.empty () {ele = stack1.top ();} return ele;} template <typename T> void MyQueue <T >:: enque (const T & ele) {stack1.push (ele) ;}template <typename T> void MyQueue <T >:: deque () {move (stack1, stack2); if (! Stack2.empty () {stack2.pop () ;}} 2. use two queues to implement the stack idea: when a new element is added to the stack, if both queues are empty, an element is inserted to the end of any queue. Otherwise, an element is inserted into one non-empty queue. When the stack pop-up elements, the non-empty queue elements are deleted in sequence, inserted into another empty queue, leaving only the end elements (this is the top element of the stack ), the top of the stack pops up to delete this element from the queue. [Cpp] template <typename T> class MyStack {public: T top (); void push (const T & ele); void pop (); private: std :: queue <T> queue1; std: queue <T> queue2 ;}; template <typename T> T MyStack <T >:: top () {T ele; if (queue1.empty ()&&! Queue2.empty () {ele = queue2.back ();} else if (! Queue1.empty () & queue2.empty () {ele = queue1.back ();} return ele;} template <typename T> void MyStack <T>: push (const T & ele) {if (queue1.empty () {queue2.push (ele);} else if (queue2.empty () {queue1.push (ele);} template <typename T> void MyStack <T>:: pop () {if (queue1.empty () {while (queue2.size ()> 1) {queue1.push (queue2.front (); queue2.pop ();} if (! Queue2.empty () {queue2.pop () ;}} else if (queue2.empty () {www.2cto.com while (queue1.size ()> 1) {queue2.push (queue1.front ()); queue1.pop ();} if (! Queue1.empty () {queue1.pop ();}}}

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.