20200927:JavaとCppのスタックとキューの違い

トピック

1.225。
キューを使用してスタックを実装する
2.232。スタックを使用してキューを実装する3.155。最小限のスタック

アイデアとアルゴリズム

  1. 3つの簡単な演習は以前にJavaで提出されましたが、今回はCppで提出され、2つのstlまたは対応するutilツールクラスのメソッドの使用を深めています。

コード

1.225。キューを使用したスタックの実装

class MyStack {
    
    
public:
    /** Initialize your data structure here. */
    MyStack() {
    
    
    }
    
    /** Push element x onto stack. */
    void push(int x) {
    
    
        std::queue<int> tmp_queue;
        tmp_queue.push(x);
        while (!_data.empty()) {
    
    
            tmp_queue.push(_data.front());
            _data.pop();
        }
        while (!tmp_queue.empty()) {
    
    
            _data.push(tmp_queue.front());
            tmp_queue.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
    
    
        int x = _data.front();
        _data.pop();
        return x;
    }
    
    /** Get the top element. */
    int top() {
    
    
        return _data.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
    
    
        return _data.empty();
    }
private:
    std::queue<int> _data;
};

/**
 * 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();
 */

2.232。スタックを使用したキューの実装

class MyQueue {
    
    
public:
    /** Initialize your data structure here. */
    MyQueue() {
    
    
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
    
    
        std::stack<int> tmp_stack;
        while (!_data.empty()) {
    
    
            tmp_stack.push(_data.top());
            _data.pop();
        }
        tmp_stack.push(x);
        while (!tmp_stack.empty()) {
    
    
            _data.push(tmp_stack.top());
            tmp_stack.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
    
    
        int x = _data.top();
        _data.pop();
        return x;
    }
    
    /** Get the front element. */
    int peek() {
    
    
        return _data.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
    
    
        return _data.empty();
    }
private:
    std::stack<int> _data;
};

/**
 * 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();
 */

3.155。最小スタック

class MinStack {
    
    
public:
    /** initialize your data structure here. */
    MinStack() {
    
     
    }
    
    void push(int x) {
    
    
        _data.push(x);
        if (minStack.empty()) {
    
    
            minStack.push(x);
        } else {
    
    
            if (x > minStack.top()) {
    
    
                x = minStack.top();
            }
            minStack.push(x);
        }
    }
    
    void pop() {
    
    
        _data.pop();
        minStack.pop();
    }
    
    int top() {
    
    
       return  _data.top();
    }
    
    int getMin() {
    
    
       return minStack.top(); 
    }
private:
    std::stack<int> _data;
    std::stack<int> minStack;
};

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

2つの言語のスタックとキューの違いを比較して分析します

  1. スタック間の違い
    ここに画像の説明を挿入

  2. キュー間の違い
    ここに画像の説明を挿入

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/qq_36828395/article/details/108839334