queue_use_stack

  • 2022-12-14
  • 浏览 (536)

queue_use_stack.cpp 源码

// 使用栈实现队列

#include "stack"
#include "iostream"
using namespace std;

class MyQueue {
private:
    stack<int> output;
    stack<int> input;

public:
    MyQueue() {
    }
    
    void push(int x) {
        input.push(x);
    }
    
    int pop() {
        int res = peek();
        output.pop();
        return res;
    }
    
    int peek() {
        if (output.empty()) {
            while (!input.empty()) {
                output.push(input.top());
                input.pop();
            }
        }
        int res = output.top();
        return res;
    }
    
    bool empty() {
        return input.empty() && output.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();
 */

你可能感兴趣的文章

min_stack

valid_parentheses

0  赞