013 use two stacks to implement one queue (keep it up) and 013 keep

Source: Internet
Author: User

013 use two stacks to implement one queue (keep it up) and 013 keep
Implement a queue using two stacks
A queue is a first-in-first-out data structure (FIFO), and a stack is a first-out data structure (FILO ),
The simplest way to use two stacks to implement a queue is to press the stack to the first stack when the queue is entered,
The data of the first stack is pushed into the Second stack in sequence, and the data of the first stack is then pushed out.
Two rules:
1) into the queue, it is directly pushed into the first stack
2) output queue. If the Second stack is not empty, pop () is used directly. If the Second stack is empty,

Press all the data in the first stack into the Second stack (the first stack is empty at this time ).

When writing code, note that the stack is empty.

Code:

#include <iostream>#include <stack>class Queue{public:Queue() {}~Queue() {}void push(int vData){m_First.push(vData);}void pop(){if (m_Second.empty()){if (m_First.empty()){std::cout << "the queue is empty" << std::endl;return ;}move(m_First, m_Second);m_Second.pop();}}int top(){if (m_Second.empty()){if (m_First.empty()){std::cout << "the queue is empty" << std::endl;return ;}move(m_First, m_Second);}return m_Second.top();}int back(){if (m_First.empty()){if (m_Second.empty()){std::cout << "the queue is empty" << std::endl;return ;}move(m_Second, m_First);}return m_First.top();}private:void move(std::stack<int>& vL, std::stack<int>& vR){while (!vL.empty()){vR.push(vL.top());vL.pop();}}private:std::stack<int> m_First;std::stack<int> m_Second;};



Can I use two stacks to implement a queue? Algorithms and ideas

For example, let's take the following four steps:
Push 1, 2
Pop // pop 1 at this time
Push 3
Pop // pop 2 at this time

When running the first pop, push all 1 and 2 in A to B, and then pop to get 1. At this time, 2 is left in B.
Push 3 next, push to
In the last step, pop 2 of B.

Key points:
(2) If it is not empty, all elements in stack A will pop out and push to stack B in sequence;

This implies that, if it is null, pop is directly from B and no operation is performed on. Obviously, the if. else statement is required.

A stack is different from a general outbound stack. You need to check whether B is empty.
If B is not empty, the stack is directly exported from B, which is the same as the general outbound stack.
If B is empty, all the elements of A need to be pushed out of the stack and stack to B, and then perform general out-of-stack operations on B.

How to implement a queue for two stacks in C Language

Pseudo Code

Join:
InQueue (elem ):

While (stack A is not empty)
Stack Bpush (stack Apop );

Stack Apush (elem );

While (stack B is not empty)
Stack Apush (stack Bpop );

Team-out
OutQueue ():
Stack Apop

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.