如何使用堆栈反转双端队列?

问题描述 投票:0回答:1

这是我的代码:-

stack <int> intStack;

    while (!adaqueue.empty())
    {
        intStack.push(adaqueue.front());
        adaqueue.pop_front();
    }
    while (!intStack.empty())
    {
        adaqueue.push_front(intStack.top());
        intStack.pop();
    }

没有提供正确的输出。例如,双端队列中的项目分别为12和45。如果我使用上面的代码来反转双端队列,则会给出错误的输出。如果我尝试打印前面的项目,它将给我12而不是45。代码的潜在错误是什么?N.B.用c ++语言编写的代码。

c++ deque
1个回答
0
投票

[您要做的是,在第二个while循环中,您要从堆栈中删除顶部,然后将其添加到队列的后面。这样,您将冲销订单。尝试在纸上画草图。这将有助于对其进行可视化。

© www.soinside.com 2019 - 2024. All rights reserved.