86-Implement Stack

-225. Implement Stack using Queues My Submissions QuestionEditorial Solution
Total Accepted: 40138 Total Submissions: 131270 Difficulty: Easy
Implement the following operations of a stack using queues.

push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

用队列来实现栈:
以下给出两种实现方式来分析下优劣:
第一种方式:
我们总是将队列1当成用来插入最新元素的队列
那么
1.push:在队1push,用队1保存最新入队的元素
2.pop:队1有元素的话,队1的元素进入队2,(除最后一个元素返回外)
3.top:设计中的队1为最新的元素,如果队1不为空,则出队依次进入队2,返回最后一个元素;如果队1为空,队2的元素出队进入队1返回最后一个元素
3.empty:只有两队都为空的时候整个栈为空

缺点:这里只是将另一个队当成缓冲来使用,所以在出栈和取栈顶元素需要反复出队取得最后一个元素,然而这两个操作是高频操作,设计不合理,
优点:这里push,和empty比较简单可判定

第二种方式:
如何保证栈序:
假设 f(n1) 为n-1个元素的栈序,新来 an
那么 f(n)=anf(n1) 为栈序
这样push的方案就是先在一个空队入队元素a_n,然后把f(n-1)依次入队即可
如果想使得top和pop都很简单的话,我们就必须把最新进来的元素放在一个队的队首,并且队1优先放入
1.push:如果队1为空,则新元素入队,然后队2元素依次入队1
如果队1不为空,将队1依次入队2(此时队2为空),新元素入队1,队2元素全入队1(保证元素为栈序)
2.pop:如果队1不为空队1队首出队,否则队2队首出队
3.top:如果队1不为空返回队1队首,否则返回队2队首
3.empty:只有两队都为空的时候整个栈为空

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        q1.push_back(x);
    }

    // Removes the element on top of the stack.
    void pop() {
        int last;
        if(q1.empty()){
            while(!q2.empty()){
                last = q2.front();   
                q2.pop_front();
                if(!q2.empty())
                    q1.push_back(last);
            }
        }
        else {
            while(!q1.empty()){
                last = q1.front();  
                q1.pop_front();
                if(!q1.empty())q2.push_back(last);
            }
        }
    }

    // Get the top element.
    int top() {
        int last;
        if(q1.empty()){
            while(!q2.empty()){
                last = q2.front();   
                q1.push_back(last);
                q2.pop_front();
            }
            return last;
        }
        else {
            while(!q1.empty()){
                last = q1.front();   
                q2.push_back(last);
                q1.pop_front();
            }
            return last;
        }
    }

    // Return whether the stack is empty.
    bool empty() {
        return q1.empty()&&q2.empty();
    }
private:
    deque<int> q1,q2;
};
class Stack {
public:
    // Push element x onto stack.
    queue<int> queue1;
    queue<int> queue2;
    void push(int x) {
        if (queue1.empty())
        {
            queue1.push(x);
            while(!queue2.empty()){
                int tmp = queue2.front();
                queue2.pop();
                queue1.push(tmp);
            }
        }else{
            queue2.push(x);
            while(!queue1.empty()){
                int tmp = queue1.front();
                queue1.pop();
                queue2.push(tmp);
            }
        }
    }

    // Removes the element on top of the stack.
    void pop() {
        if (!queue1.empty())
            queue1.pop();
        if (!queue2.empty())
            queue2.pop();
    }

    // Get the top element.
    int top() {
        if (!queue1.empty())
            return queue1.front();
        if (!queue2.empty())
            return queue2.front();
    }

    // Return whether the stack is empty.
    bool empty() {
        return queue1.empty() && queue2.empty();
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值