Implementing Stacks Using Queues

Source: Internet
Author: User

Implement the following operations of a stack using queues.

    • Push (x)-push element x onto stack.
    • Pop ()--Removes the element on top of the stack.
    • Top ()--Get the top element.
    • Empty ()--Return whether the stack is empty.

Notes:

    • You must the only standard operations of a queue – which means only push to back ,, peek/pop from front size , and is empty operations AR E valid.
    • Depending on your language, queue May is not supported natively. You could simulate a queue by using a list or deque (double-ended queue), as long as if you have standard operations of a Q Ueue.
    • You may assume this all operations is valid (for example, no pop or top operations would be called on an empty stack).

Update (2015-06-11):
The class name of the Java function had been updated to Mystack instead of Stack.

analyse:using queues to store the information. The used to mock the stack and the used to store temporary values.

1. Time Exceeded Limit Version

It ' s because transfering temporary queue to the original queue costs a lot of time.

1 classStack {2 Private:3queue<int>Q;4queue<int>Help ;5  Public:6     //Push element x onto stack.7     voidPushintx) {8 Q.push (x);9     }Ten  One     //removes the element on top of the stack. A     voidpop () { -          while(Q.size () >1){ - Help.push (Q.front ()); the Q.pop (); -         } - Q.pop (); -          while(!Help.empty ()) { + Q.push (Help.front ()); -         } +     } A  at     //Get the top element. -     intTop () { -          while(Q.size () >1){ - Help.push (Q.front ()); - Q.pop (); -         } in         intresult =Q.front (); -          while(!Help.empty ()) { to Q.push (Help.front ()); +         } -     } the  *     //Return Whether the stack is empty. $     BOOLempty () {Panax Notoginseng         returnq.empty (); -     } the};
View Code

2. Set a 2-size array to respectively represent the When it comes to copy the temporary queue to the original queue, just switch the index.

Runtime:0ms.

1 classStack {2 Private:3queue<int> q[2];4     intindex =0;5  Public:6     //Push element x onto stack.7     voidPushintx) {8 Q[index].push (x);9     }Ten  One     //removes the element on top of the stack. A     voidpop () { -          while(Q[index].size () >1){//Transfer all and elements into the helper queue -q[1-Index].push (Q[index].front ()); the Q[index].pop (); -         } - Q[index].pop (); -index =1-index; +     } -  +     //Get the top element. A     intTop () { at          while(Q[index].size () >1){//Transfer all and elements into the helper queue -q[1-Index].push (Q[index].front ()); - Q[index].pop (); -         } -         intresult =Q[index].front (); -q[1-Index].push (result); in Q[index].pop (); -index =1-index; to         returnresult; +     } -  the     //Return Whether the stack is empty. *     BOOLempty () { $         returnq[index].empty ();Panax Notoginseng     } -};

Implementing Stacks Using Queues

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.