Use two stacks to implement queues

Source: Internet
Author: User

Question: Use two stacks to implement a queue. The queue declaration is as follows. Implement two functions: appendtail and deletehead to insert nodes at the end of the queue and delete nodes at the queue header.

Ideas:First, we must be clear that the first thing we should think of when looking at this question is to use C ++. Why? Because we can see the stack and queue, if we use C for implementation, we need to first implement functions such as push and pop, and C ++ has already implemented this series of functions for us, we can call them directly.

Let's further consider the implementation. Stack is the structure of "back-in-first-out (or first-out, whatever)", while queue is the structure of "first-in-first-out. For example, we input 1 2 3 4 5 6, the stack output sequence is 6 5 4 3 2 1, and the queue output sequence is 1 2 3 4 5 6, that is, the stack is the reverse output, while the queue is the original output. Since one stack is in reverse direction, what about the two stacks? Isn't it? Yes, "reverse" is equal to "original. Readers can, and so on, use three stacks to reverse again.

How can we achieve this? We regard the two stacks (S1 and S2) as a whole, and it is a queue externally. When we input data to the entire table, the data is stored in S1, and another data is stored in S1. I will store all the data in S1. When we want to read data from this whole, we will read data from S2. The reader may ask, how do you read the data stored in S1 and there is no data in S2? This is a good problem. There is no data in S2, but there is data in S1. It can be obtained free of charge from S1. Who calls us a whole. When there is no data in S2, all the data in S1 is pop and pushed to S2.

Source code:

#include <iostream>#include <stack>using namespace std;class Queue{public:Queue(){};void appendTail(int data);int deleteHead();bool isEmpty();private:stack<int> s1;stack<int> s2;};void Queue::appendTail(int data){s1.push(data);}int Queue::deleteHead(){if(s2.empty()){while(!s1.empty()){int d =;s1.pop();s2.push(d);}}int d =;s2.pop();return d;}bool Queue::isEmpty(){if(s1.empty() && s2.empty())return true;return false;}int main(){Queue q;for(int i=0;i<10;i++)q.appendTail(i);while(!q.isEmpty())cout << q.deleteHead() << " ";}

Use two stacks to implement 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: 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.