代码随想录训练营day10打卡

用栈实现队列

232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

由于栈是后进先出,与队列的pop顺序相反,因此我们使用两个栈实现,将stackin先弹出的元素压入栈stackout,然后stackout的pop顺序就和队列一致了,MyQueue的pop实现需要注意的是,每次要弹出的时候,需要先判断stackout是否还有元素,如果有元素就直接弹出,而不是先将stackin的剩余元素压入stackout,这样会导致顺序错乱,后进入stackin的元素先pop出去。当stackout为空时,再将stackin的元素全部压入stackout,以保证顺序一致。同时由于题目要求我们的函数返回值是pop出的元素,而stack自带的pop函数返回类型为void,因此我们在pop之前需要先将stackout的top元素取出来。因此pop的逻辑是:

int pop() {
        if (stackout.empty())
        {
            while (!stackin.empty())
            {
                stackout.push(stackin.top());
                stackin.pop();
            }
        }
       int result=stackout.top();
       stackout.pop();
       return result;
    }

peek函数与pop类似,只不过peek并不会弹出元素,这里我们为了方便代码的更改一致性,就直接在peek里面掉用pop函数,然后在return之前再将该元素push到stackout即可。

    int peek() {
        int result=this->pop();
        stackout.push(result);
        return result;
    }

empty函数的实现也比较简单,直接通过判断stackin和stackout两个栈是否都为空的逻辑实现,如果都为空说明没有剩余元素。

    bool empty() {
        bool flag = false;
        if (stackout.empty()&&stackin.empty()) flag = true;
        return flag;
    }

因此完整代码:

class MyQueue {
    stack<int> stackin;
    stack<int> stackout;
public:
    MyQueue() {

    }

    void push(int x) {
        stackin.push(x);
    }

    int pop() {
        if (stackout.empty())
        {
            while (!stackin.empty())
            {
                stackout.push(stackin.top());
                stackin.pop();
            }
        }
       int result=stackout.top();
       stackout.pop();
       return result;
    }

    int peek() {
        int result=this->pop();
        stackout.push(result);
        return result;
    }

    bool empty() {
        bool flag = false;
        if (stackout.empty()&&stackin.empty()) flag = true;
        return flag;
    }
};

用队列实现栈

225. 用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop 和 empty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

与用栈实现队列类似,可以使用两个队列实现,但由于队列可以在输出的另一头进行输入,并且并不改变原来的队列顺序,因此直接使用一个队列即可实现,根据栈的定义可知,我们需要pop的元素在队尾,因此我们直接将queue的size-1个元素弹出,剩下的最后一个元素就是我们需要的元素。如果使用一个队列实现,我们直接将size-1个元素输出的顺序push进queue,队列中元素的顺序依旧和操作前一致。因此pop的代码实现:

    int pop() {
        int size = que.size();
        size--;
        while (size--)
        {
            que.push(que.front());
            que.pop();
        }
        int result=que.front();
        que.pop();
        return result;
    }

使用队列实现栈,top函数比较简单,因为栈中的top元素即为队列中的back元素,因此直接返回该队列的back即可。empty不必赘述。

    int top() {
        return que.back();
    }

    bool empty() {
        return que.empty() ? true : false;
    }

完整代码:

class MyStack {
    queue<int> que;
public:
    MyStack() {

    }

    void push(int x) {
        que.push(x);
    }

    int pop() {
        int size = que.size();
        size--;
        while (size--)
        {
            que.push(que.front());
            que.pop();
        }
        int result=que.front();
        que.pop();
        return result;
    }

    int top() {
        return que.back();
    }

    bool empty() {
        return que.empty() ? true : false;
    }
};

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值