简体   繁体   English

如何在 C++ 中使用单个堆栈实现队列

[英]How to implement queue with a single stack in C++

In this C++ code I'm implementing Queue with a Single stack instance.在此 C++ 代码中,我使用单stack实例实现Queue I found this code in GeeksForGeeks.我在 GeeksForGeeks 中找到了这段代码。 Url Here网址在这里

#include <bits/stdc++.h>
using namespace std;
class Queue
{
private:
    stack<int> s;

public:
    void enque(int x)
    {
        s.push(x);
    }
    int deque()
    {
        if (s.empty())
        {
            cout << "Q is empty" << endl;
            return -1;
        }
        int x = s.top();
        s.pop();
        if (s.empty())
        {
            return x;
        }
        // I'm not able to understand these 3 lines after this comment
        int item = deque();
        s.push(x);
        return item;
    }
};
int main()
{
    Queue q;
    q.enque(1);
    q.enque(2);
    q.enque(3);
    cout << "Output: " << q.deque() << endl;
    cout << "Output: " << q.deque() << endl;
    cout << "Output: " << q.deque() << endl;
    cout << "Output: " << q.deque() << endl;
    return 0;
}

But I cannot understand these 3 lines但我无法理解这 3 行

int item = deque();
s.push(x);
return item;

Problem问题

How after calling deque() recursively the compiler reaches the next lines to push x again to the stack.在递归调用 deque() 之后,编译器如何到达下一行将x再次压入堆栈。 And how it is retaining the value of x after recursive function call.以及它如何在递归函数调用后保留 x 的值。

The code isn't really using a single stack, its using the built-in stack as a second stack via the recursive call to deque .代码并不是真正使用单个堆栈,而是通过递归调用deque使用内置堆栈作为第二个堆栈。 The code is equivalent to:代码等价于:

#include <iostream>
#include <stack>

class Queue
{
private:
    std::stack<int> s;

public:
    void enque(int x)
    {
        s.push(x);
    }
    int deque()
    {
        if (s.empty())
        {
            std::cout << "Q is empty\n";
            return -1;
        }
        std::stack<int> temp;
        while (s.size() != 1)
        {
            temp.push(s.top());
            s.pop();
        }
        int result = s.top();
        s.pop();
        while (!temp.empty())
        {
            s.push(temp.top());
            temp.pop();
        }
        return result;
    }
};
int main()
{
    Queue q;
    q.enque(1);
    q.enque(2);
    q.enque(3);
    std::cout << "Output: " << q.deque() << "\n";
    std::cout << "Output: " << q.deque() << "\n";
    std::cout << "Output: " << q.deque() << "\n";
    std::cout << "Output: " << q.deque() << "\n";
    return 0;
}

I can't think of a reason you'd implement a queue this way, as the queue grows in size deque gets more and more expensive, using the original code you'd ultimately end up with a stack overflow.我想不出你以这种方式实现队列的原因,随着队列大小的增长, deque变得越来越昂贵,使用原始代码最终会导致堆栈溢出。 If you need a queue use std::queue or std::deque (by default std::queue is just a wrapper round std::deque which hides the push_front / pop_back methods)如果您需要队列,请使用std::queuestd::deque (默认情况下, std::queue只是std::deque的包装器,它隐藏了push_front / pop_back方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM