代码随想录算法训练营DAY10|C++栈和队列Part.1|LeetCode:232.用栈实现队列、225. 用队列实现栈

232.用栈实现队列

力扣题目链接

文章链接:225. 用队列实现栈

视频链接:栈的基本操作! | LeetCode:232.用栈实现队列

状态:考察栈的基本操作

思路

两个栈实现队列,其实思路非常简单。

初始化

先定义两个栈再说。

stack<int> stIn;
stack<int> stOut;

int push()

将元素 x 推到队列的末尾,直接压入stIn`即可

int pop(int x){
  stIn.push(x);
}

void pop()

从队列的开头移除并返回元素。两个栈来模拟队列其实就是,一个作为队列入口stIn,一个作为队列出口stout.

所以当依次压入1, 2, 3, 4,存储在stIn = [1, 2, 3, 4],要出队列的时候依次把4,3,2,1存储在stOut=[4, 3, 2,1],再进行出列队的操作,这样就用栈实现了先进先出。但有两个条件一定注意:

  • stOut为空是什么情况呢?从stIn里导入数据(导入stIn全部数据)
  • stOut不为空呢?说明栈stOut的栈顶就是我们需要的数据(这里有一点绕,但是还是很好理解。详情可看上面的文章)
void pop(){
  while (!stOut.empty()){
    stOut.push(stIn.top());
    stIn.pop();
  }
  int result = stOut.top();
  stOut.pop();
  return result;
}

int peek()

返回队列开头元素。队列开头的元素其实存储在stOut中,如果它为空,那么就把stIn的数据导入。

int peek() {
    int res = this->pop(); // 直接使用已有的pop函数
    stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
    return res;
}

直接代码复用this->pop()

boolean empty()

  • boolean empty() 如果队列为空,返回 true ;否则,返回 false
  • 其实只要两个栈都空,那队列就为空
bool empty() {
    return stIn.empty() && stOut.empty();
}

CPP代码

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    /** Initialize your data structure here. */
    MyQueue() {

    }
    /** Push element x to the back of queue. */
    void push(int x) {
        stIn.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        // 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
        if (stOut.empty()) {
            // 从stIn导入数据直到stIn为空
            while(!stIn.empty()) {
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }

    /** Get the front element. */
    int peek() {
        int res = this->pop(); // 直接使用已有的pop函数
        stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
        return res;
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

225. 用队列实现栈

力扣题目链接(opens new window)

文章链接:225. 用队列实现栈

视频链接:队列的基本操作! | LeetCode:225. 用队列实现栈

状态:考察队列的基本操作,这里的逻辑有点那那个,建议两个队列模拟栈和一个队列模拟栈都写一写

思路

两个队列模拟栈

两个队列模拟栈的基本思路就是,进入队列queue1入口:[3, 2, 1]:出口。先入队的是1,我们现在想弹出3。那就把12先放到另一个队列queue2,然后queue1弹出3即可,然后想弹出2,就要12回到queue1,然后1到queue2,再弹出2

详细介绍可看上文中给出的文章链接和视频链接

初始化
queue<int> que1;
queue<int> que2; // 辅助队列,用来备份
void push(int x)
void push(int x){
  que1.push(x);
}
int pop()

pop的逻辑就跟上文将的一样,但是一定要记得清空que2,为下一次pop做好准备。

int pop(){
  int size = que1.size();
  size--;
  while (size--){	// 将que1 导入que2,但要留下最后一个元素
    que2.push(que1.ftonr());
    que1.pop();
  }
  
  int result = que1.front();	//	留下的最后一个元素就是要返回的值
  que1.pop();
  que1 = que2;	//直接将que2赋值给que1
  while (!que2.empty()){	//清空que2
    que2.pop();
  }
  return result;
}
int top()
int top(){
  return que1.back();
}
boolean empty()
bool empty(){
  return que1.empty();
}

一个队列模拟栈

其实就是把前面的元素弹出,然后再到队尾,这样队列最前面的元素就变成了我们想要弹出的那个元素。所以如果length(queue) = size。我们就把前面size - 1个元素弹出即可。

一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。

初始化
queue<int> que;
void push(int x)
void push(int x){
  que.push(x);
}
int pop()
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()
int top(){
  return que.back();
}
bool empty()
bool empty(){
  return que.empty();
}

CPP代码

两个队列模拟栈

class MyStack {
public:
    queue<int> que1;
    queue<int> que2; // 辅助队列,用来备份
    /** Initialize your data structure here. */
    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        que1.push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que1.size();
        size--;
        while (size--) { // 将que1 导入que2,但要留下最后一个元素
            que2.push(que1.front());
            que1.pop();
        }

        int result = que1.front(); // 留下的最后一个元素就是要返回的值
        que1.pop();
        que1 = que2;            // 再将que2赋值给que1
        while (!que2.empty()) { // 清空que2
            que2.pop();
        }
        return result;
    }

    /** Get the top element. */
    int top() {
        return que1.back();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return que1.empty();
    }
};

一个队列模拟栈

class MyStack {
public:
    queue<int> que;
    /** Initialize your data structure here. */
    MyStack() {

    }
    /** Push element x onto stack. */
    void push(int x) {
        que.push(x);
    }
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que.size();
        size--;
        while (size--) { // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
            que.push(que.front());
            que.pop();
        }
        int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
        que.pop();
        return result;
    }

    /** Get the top element. */
    int top() {
        return que.back();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return que.empty();
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值