Mutual realization of stack and queue

1 Stack and queue

Stack: last in first out data structure
Queue: first in first out data structure
as shown in the figure:

Insert picture description here

So take C++ as an example, what are the stacks and queues in the programming language you use? You can think about the following aspects:

  1. Is stack in C++ a container?
  2. Which version of STL does the stack we use belong to ?
  3. How do we use stack in STL to achieve it?
  4. Does stack provide iterators to traverse the stack space?

There are multiple versions of the C++ standard library. You need to know which version of the STL we are using to know the implementation principle of the corresponding stack and queue. We use SGI STL, which is referred to by Silicon Graphics Computer Systems from HP STL. Implementation, adopted by Linux C++ compiler GCC, SGI STL is open source software, the source code is very readable.
Let's talk about the stack
Insert picture description here

  1. The stack provides interfaces such as push and pop. All elements must comply with the first-in-last-out rule, so the stack does not provide traversal functions, nor does it provide iterators. Unlike set or map, it provides an iterator to traverse all elements.
  2. The stack is based on the bottom container to complete all the work, providing a unified interface to the outside, and the bottom container is pluggable-we can control which container is used to implement the function of the stack. Therefore, the stack in STL is not classified as a container, but a container adapter.

So what container is used to implement the stack in STL?

As can be seen from the figure below, the internal structure of the stack, the underlying implementation of the stack can be vector, deque, list are all possible, mainly the underlying implementation of arrays and linked lists,
Insert picture description here
we commonly use SGI STL, if the underlying implementation is not specified, The default is deque as the low-level structure of the stack by default. A deque is a two-way queue. As long as one section is sealed, the logic of the stack can be realized only by opening the other end.

After the stack is explained, the queue is easier to understand?

The first-in-first-out data structure of the queue also does not allow traversal behavior, and does not provide iterators. The queue in SGI STL uses deque as the default bottom structure.
Therefore, queues in STL are not classified as containers, but as container adapters.

3 Use the stack to implement the queue

Insert picture description here
https://leetcode-cn.com/problems/implement-queue-using-stacks/

3.1 Thinking analysis

Using stacks to model the behavior of queues, if you only use one stack, it will definitely not work, so you need two stacks, one input stack and one output stack. Here we must pay attention to the relationship between the input stack and the output stack.
The following quotes the animation of the big guy, https://mp.weixin.qq.com/s/P6tupDwRFi6Ay-L7DT4NVg

3.2 Code implementation

class MyQueue {
    
    
    //用两个栈实现队列
public:
    stack<int> stackIn;
    stack<int> stackOut;
    /** Initialize your data structure here. */
    MyQueue() {
    
    

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
    
    
        stackIn.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
    
    
       //当stackOut中是空的时候把stackIn中的数据导入到stackOut中
       if (stackOut.empty()) {
    
    
           //把stackIn中的数据导入,直到其为空为止
           while (!stackIn.empty()) {
    
    
               stackOut.push(stackIn.top());
               stackIn.pop();
           }
       }
       int result = stackOut.top();
       stackOut.pop();
       return result;
    }
    
    /** Get the front element. */
    int peek() {
    
    
        int ret = this->pop();
        stackOut.push(ret);
        return ret;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
    
    
       return stackIn.empty() && stackOut.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

4 Use queues to implement stacks

Insert picture description here
https://leetcode-cn.com/problems/implement-stack-using-queues/

4.1 Thinking analysis

The above is to use a stack to implement a queue, an input stack, and an output stack. Is it possible to use a queue to implement a stack? In fact, it does not work. The queue follows the first-in-first-out rule, and the data in one queue is imported into another queue. In, the order of the data has not changed, and it has become first in last out.
So the idea of ​​using queues to implement stacks can be: two queues and one queue is used as a backup. Two queues, que1 and que2, are used to implement the function of the queue. que2 is actually a backup function. All elements other than the last element of que1 are backed up to que2, then the last element is popped, and the other elements are imported back from que2. que1.
Quote the big guy’s animated picture: https://mp.weixin.qq.com/s/yzn6ktUlL-vRG3-m5a8_Yw

4.2 Code implementation

class MyStack {
    
    
public:
    //用两个队列实现栈的功能----后进先出
    queue<int> que1;
    queue<int> que2;//用于临时备份数据
    /** Initialize your data structure here. */
    MyStack() {
    
    

    }
    
    /** Push element x onto stack. */
    void push(int x) {
    
    
        que1.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
    
    
        //将que1中的数据放入到que2中,直到que1中只剩1个数据
        while (que1.size() > 1) {
    
    
            que2.push(que1.front());
            que1.pop();
        }
        //此时que1中有一个数据
        int ret = que1.front();
        que1.pop();
        //把que2中的数据放回que1中
        while(!que2.empty()) {
    
    
            que1.push(que2.front());
            que2.pop();
        }
        return ret;
    }
    
    /** Get the top element. */
    int top() {
    
    
        return que1.back();//back:队列尾   front:队列头
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
    
    
        return que1.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

Guess you like

Origin blog.csdn.net/CZHLNN/article/details/113849343
Recommended