Little-Prince

导航

232. 用栈实现队列&225. 用队列实现栈

232. 用栈实现队列

 

使用栈实现队列的下列操作:

 push(x) -- 将一个元素放入队列的尾部。
 pop() -- 从队列首部移除元素。
 peek() -- 返回队列首部的元素。
 empty() -- 返回队列是否为空。
 
 
 
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2); 
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 false
 
 
 
说明:

 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
 
 
思路:利用两个栈,一个input,储存进入的元素,一个output,对于出栈的元素先到 output,再执行出栈操作。负负得正,栈可以变成队列。
 
代码:
class MyQueue {
    stack<int> input;
    stack<int> output;
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        input.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int temp;
        if(output.empty())
            {
                while(!input.empty())
                {
                    temp = input.top();
                    output.push(temp);
                    input.pop();
                }
            }
        temp = output.top();
        output.pop();
        return temp;
    }
    
    /** Get the front element. */
    int peek() {
        int temp;
        if(output.empty())
            {
                while(!input.empty())
                {
                    temp = input.top();
                    output.push(temp);
                    input.pop();
                }
            }
        temp = output.top();
        return temp;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        if(input.empty()&&output.empty())
            return 1;
        else
            return 0;


    }
};

/**
 * 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. 用队列实现栈

使用队列实现栈的下列操作:

 push(x) -- 元素 x 入栈
 pop() -- 移除栈顶元素
 top() -- 获取栈顶元素
 empty() -- 返回栈是否为空

注意:

 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
 
 
思路:仅使用一个队列,
当我们将一个元素从队列入队的时候,根据队列的性质这个元素会存在队列的后端。
但当我们实现一个栈的时候,最后入队的元素应该在前端,而不是在后端。为了实现这个目的,每当入队一个新元素的时候,我们可以把队列的顺序反转过来。即将队列前面的元素删除添加到队列后面。
 
代码:
class MyStack {
    queue<int> st;
public:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
        st.push(x);
        int len = st.size();
        while(len > 1)
        {
            st.push(st.front());
            st.pop();
            len--;
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int temp = st.front();
        st.pop();
        return temp;
    }
    
    /** Get the top element. */
    int top() {
        return st.front();

    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return st.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();
 */

 

 

posted on 2020-07-30 23:45  Little-Prince  阅读(122)  评论(0)    收藏  举报