0
点赞
收藏
分享

微信扫一扫

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

兵部尚输 2022-03-30 阅读 65

232. 用栈实现队列

题目描述:

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

实现 MyQueue 类:

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

你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

解答:

使用两个栈,一个栈用于入队(stIn),一个用于出队(stOut)。入队时正常stIn.push()即可。因为队列要求先进先出,和栈相反,出队时先看stOut中有无数据,有则直接stOut出栈即可,如果没有则需要将stIn中的所有元素出栈并压栈至stOut中(相当于一个取反操作,实现先进先出)

代码实现:

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    MyQueue() {
        
    }
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {
        if (stOut.empty())
        {
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int x = stOut.top();
        stOut.pop();
        return x;
    }
    
    int peek() {
        if (stOut.empty())
        {
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        return stOut.top();
    }
    bool empty() {
        if (stOut.empty() && stIn.empty())
            return true;
        else
            return false;
    }
};

225. 用队列实现栈

题目描述:

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

实现 MyStack 类:

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

注意:

你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

解答:和栈实现队列类似,设置两个队列quIn和quOut。push操作简单直接放入quIn即可。pop时先从quIn中取(因为栈是后进先出的),如果quIn不为空,取quIn中的最后一个元素,其他元素放入quOut中,如果quIn为空从quOut中取最后一个即可,其他元素再放入quIn中。

代码实现:

class MyStack {
public:
    queue<int>quIn, quOut;
    MyStack() {

    }
    
    void push(int x) {
        quIn.push(x);
    }
    
    int pop() {
        if (!quIn.empty()){
            int x;
            while(1){
                x = quIn.front();
                quIn.pop();  
                if (!quIn.empty())
                    quOut.push(x);
                else
                    break;
            }
            return x;
        }
        else{
            int y;
            while(1){
                y = quOut.front();
                quOut.pop();
                if (!quOut.empty())
                    quIn.push(y);
                else    
                    break;
            }
            return y;
        }
    }
    
    int top() {
        if (!quIn.empty()){
            int x;
            while(!quIn.empty()){
                x = quIn.front();  
                quIn.pop();
                quOut.push(x);
            }
            return x;
        }
        else{
            int y;
            while(!quOut.empty()){
                y = quOut.front();
                quOut.pop();
                quIn.push(y);
            }
            return y;
        }
    }
    bool empty() {
        if (quIn.empty() && quOut.empty())
            return true;
        return false;
    }
};
举报

相关推荐

0 条评论