当前位置:网站首页>407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)

407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)

2022-06-23 07:07:00 liufeng2023

232. Using stack to realize queue

 Insert picture description here

class MyQueue {
    
public:

    stack<int> st_in;
    stack<int> st_out;

    MyQueue() {
    

    }

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

    int pop() {
    
        if (st_out.empty())
        {
    
            while (!st_in.empty())
            {
    
                st_out.push(st_in.top());
                st_in.pop();
            }
        }

        int result = st_out.top();
        st_out.pop();
        return result;
    }

    int peek() {
    
        int res = this->pop();
        st_out.push(res);
        return res;
    }

    bool empty() {
    
        return st_in.empty() && st_out.empty();
    }

};

 Insert picture description here

225. Using queue to implement stack

Method 1

class MyStack {
    
public:
    queue<int> que1;
    queue<int> que2;
public:
    MyStack() {
    

    }

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

    int pop() {
    
        int size = que1.size();
        size--;

        while (size--)    take que1  Import que2, But leave the last element 
        {
    
            que2.push(que1.front());
            que1.pop();
        }

        int result = que1.front();//  The last element left is the value to return 
        que1.pop();
        que1 = que2; //  then que2 Assign a value to que1
        while (!que2.empty()) // Empty que2
        {
    
            que2.pop();
        }
        return result;
    }

    int top() {
    
        return que1.back(); // The top element of the stack is que1 The last element inserted !
    }

    bool empty() {
    
        return que1.empty();
    }
};

 Insert picture description here

Optimize : Method 2

In fact, this topic uses A line That's enough .

class MyStack {
    
public:
    queue<int> que;
    /** Initialize your data structure here. */
    MyStack() {
    

    }
    /** Push element x onto stack. */
    void push(int x) {
    
        que.push(x);
    }
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
    
        int size = que.size();
        size--;
        while (size--) {
     //  The element of the queue header ( Except for the last element )  Re add to the end of the queue 
            que.push(que.front());
            que.pop();
        }
        int result = que.front(); //  At this point, the order of the pop-up elements is the order of the stack 
        que.pop();
        return result;
    }

    /** Get the top element. */
    int top() {
    
        return que.back();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
    
        return que.empty();
    }
};

 Insert picture description here

原网站

版权声明
本文为[liufeng2023]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230615585614.html