代码随想录day10 栈实现队列 队列实现栈

232. 用栈实现队列 - 力扣(LeetCode) 

Python: 用两个列表stack_in和stack_out来模拟两个栈。两个栈即可实现队列。入队push时入栈stack_in。弹出队头元素pop(返回并删除)时,由于栈是后进先出的,需要将stack_in的元素挪到stack_out,使得元素倒序,这样从stack_out末尾取出的元素就是队列头部的元素。而peek只返回队头元素但不删除:需要调用一次self.pop(),再将弹出元素放回队列头部,即stack_out栈的尾部,即stack_out.append(res)。

class MyQueue:

    def __init__(self):
        self.stack_in = []
        self.stack_out = []


    def push(self, x: int) -> None:
        self.stack_in.append(x)


    def pop(self) -> int:
        if self.empty():
            return None

        if self.stack_out:
            return self.stack_out.pop() #python list pop(), index default=-1

        if not self.stack_out:
            while self.stack_in:
                self.stack_out.append(self.stack_in.pop())

            return self.stack_out.pop()


    def peek(self) -> int:
        res = self.pop()
        self.stack_out.append(res)
        return res


    def empty(self) -> bool:
        if self.stack_in or self.stack_out:
            return False
        return True

C++:

class MyQueue {
public:
    stack<int> in;
    stack<int> out;

    MyQueue() {
    }
    
    void push(int x) {
        in.push(x);
    }
    
    int pop() {
        if (out.empty()) {
            while (!in.empty()) {
                int tmp = in.top();
                in.pop();
                out.push(tmp);
            }
        }

        int res = out.top();
        out.pop();
        return res;
    }
    
    int peek() {
        int res = this->pop();
        out.push(res);
        return res;
    }
    
    bool empty() {
        if (in.empty() && out.empty()) {
            return true;
        }
        return false;
    }
};

也可以用链表: 

class node:
    def __init__(self, val = -1, next = None, pre = None):
        self.val = val
        self.next = next
        self.pre = pre


class MyQueue:

    def __init__(self):
        dum_head = node()
        dum_tail = node(pre = dum_head)
        dum_head.next = dum_tail
        self.dum_head = dum_head
        self.dum_tail = dum_tail


    def push(self, x: int) -> None:
        tmp = self.dum_tail.pre
        new_node = node(val = x, next = self.dum_tail, pre = tmp)
        tmp.next = new_node
        self.dum_tail.pre = new_node


    def pop(self) -> int:
        pop_node = self.dum_head.next
        self.dum_head.next = pop_node.next
        pop_node.next.pre = self.dum_head
        return pop_node.val


    def peek(self) -> int:
        return self.dum_head.next.val


    def empty(self) -> bool:
        if self.dum_head.next == self.dum_tail:
            return True
        return False

225. 用队列实现栈 - 力扣(LeetCode)

C++:两个队列

class MyStack {
public:
    queue<int> q1;
    queue<int> q2;
    MyStack() {
    }
    
    void push(int x) {
        if (!q1.empty()) {
            q1.push(x);
        }
        else {
            q2.push(x);
        }
    }
    
    int pop() {
        if (q1.empty()) {
            int cur = q2.front();
            q2.pop();
            while (!q2.empty()) {
                q1.push(cur);
                cur = q2.front();
                q2.pop();
            }
            return cur;
        }
        else {
            int cur = q1.front();
            q1.pop();
            while (!q1.empty()) {
                q2.push(cur);
                cur = q1.front();
                q1.pop();
            }
            return cur;
        }
    }
    
    int top() {
        int res = this->pop();
        if (!q1.empty()) {
            q1.push(res);
        }
        else {
            q2.push(res);
        }

        return res;
    }
    
    bool empty() {
        if (q1.empty() && q2.empty()) {
            return true;
        }
        return false;
    }
};

C++:简化为用一个队列

pop:要求弹出最后一个元素,将最后一个元素之前的所有元素依次删除并重新入队,此时队列的首个元素即原来的最后一个元素。queue.pop()删去首个元素。

top:调用一次pop方法,再使最后一个元素重新入队以保证队列不改变。

class MyStack {
public:
    queue<int> q;
    MyStack() {
    }
    
    void push(int x) {
        q.push(x);
    }
    
    int pop() {
        int s = q.size();
        for (int i = 0; i < s - 1; i++) {
            int tmp = q.front();
            q.pop();
            q.push(tmp);
        }
        int res = q.front();
        q.pop();
        return res;
    }
    
    int top() {
        int res = this->pop();
        q.push(res);
        return res;
    }
    
    bool empty() {
        if (q.empty()) {
            return true;
        }
        return false;
    }
};

Python:用一个队列模拟栈

from collections import deque
class MyStack(object):

    def __init__(self):
        self.q = deque()

    def push(self, x):
        self.q.append(x)


    def pop(self):
        for _ in range(len(self.q) - 1):
            self.q.append(self.q.popleft())
        return self.q.popleft()


    def top(self):
        res = self.pop()
        self.q.append(res)
        return res


    def empty(self):
        return not self.q

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值