Code Random Recording Algorithm Training Camp 15th Day 10 | Theoretical Basis, 232. Implementing Queues with Stacks, 225. Implementing Stacks with Queues

theoretical basis

Queues are first in first out and stacks are first in last out.

Four questions:

  1. Is stack a container in C++?
  2. Which version of the STL does the stack we use belong to?
  3. How is the stack implemented in the STL we use?
  4. Does stack provide an iterator to traverse the stack space?

Stack and queue are two data structures in STL (C++ Standard Library).

The stack provides interfaces such as push and pop, and all elements must conform to the first-in, last-out rule, so the stack does not provide the visit function, nor does it provide iterators. Unlike set or map, iterator iterator is provided to traverse all elements.

The stack uses the underlying container to complete all its work, and provides a unified interface to the outside world. The underlying container is pluggable (that is to say, we can control which container is used to implement the function of the stack)

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 the array and linked list.

232. Implement a queue with a stack

Queues are first in first out and stacks are first in last out.

I have learned it before, and I have long forgotten it. Here you need to pay attention to three concepts, namely push, pop, and top. If you understand these three concepts, you can perform subsequent operations. The code looks like this:

class MyQueue {
public:
    //首先声明两个栈,一个入栈一个出栈
    stack<int> in;
    stack<int> out;

    MyQueue() {

    }
    
    void push(int x) {
        //入栈过程
        in.push(x);

    }
    
    int pop() {
        //首先判断出栈是否为空
        while(out.empty())
        {
            //判断入栈是否为空,在不为空的情况下
            while(!in.empty())
            {
                out.push(in.top());
                in.pop();
            }
        }
        int result = out.top();
        out.pop();
        return result;

    }
    
    int peek() {
        int result1 = this->pop();
        out.push(result1);
        return result1;

    }
    
    bool empty() {
        return in.empty()&&out.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();
 */

225. Implement a stack with a queue

The process of popping up elements is to pop out the previous size()-1, and then add it later.

This code is relatively simple and runs successfully once.

class MyStack {
public:

    //首先声明队列
    queue<int> que;

    MyStack() {

    }
    
    void push(int x) {
        que.push(x);

    }
    
    int pop() {
        int queSize = que.size();

        queSize = queSize - 1;//首先将数目减一操作

        while(queSize--)
        {
            que.push(que.front());
            que.pop();
        }

        int result = que.front();

        que.pop();

        return result;
    }
    
    int top() {

        return que.back();

    }
    
    bool empty() {
        return que.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/m0_47489229/article/details/131002490