简体   繁体   English

我正在尝试使用两个队列实现堆栈,但我的“推送”操作有问题,尽管代码似乎没问题

[英]I am trying to implement a stack using two Queues but my "push" operation is problematic although the code seems to be fine

Here's the code for class "StackUsingTwoQueues"这是 class“StackUsingTwoQueues”的代码

The Class "queueUsingLL" is working fine and following are the methods used for this class Class“queueUsingLL”工作正常,以下是用于此 class 的方法

  1. isEmpty() returns true or false depending upon whether queue is empty or NOT isEmpty() 根据队列是否为空返回 true 或 false
  2. size() returns the integer value for size of the queue size() 返回队列大小的 integer 值
  3. enqueue(int element) stores the element at the tail of the queue enqueue(int element) 将元素存入队列尾部
  4. dequeue() removes the value stored at the front of queue and returns it dequeue() 删除存储在队列前面的值并返回它

Using this queue class I tried to implement the stack使用这个队列 class 我试图实现堆栈

class StackUsingTwoQueues {
    QueueUsingLL<int> q;
    QueueUsingLL<int> tempQ;
    

    public:
        bool isEmpty(){
            return q.isEmpty();
        }
        int pop(){
            if(isEmpty()){
                return -1;
            }
            else{
                return q.dequeue();
            }
        }
        int size(){
            return q.size();
        }

// following push method is problematic : -

        void push(int element){
            if(q.isEmpty()){
                q.enqueue(element);
            }
            else{
                for(int i = 0; i < q.size(); i++){
                    tempQ.enqueue(q.dequeue());
                }
                q.enqueue(element);
                for(int i = 0; i < tempQ.size(); i++){
                    q.enqueue(tempQ.dequeue());
                }
            }
        }

        int top(){
            return q.front();
        }
};

PLEASE NOTE THAT if I write following code for push() function, it works fine: -请注意,如果我为 push() function 编写以下代码,它可以正常工作:-

void push(int element){
            while(!q.isEmpty()){
                tempQ.enqueue(q.dequeue());
            }
            q.enqueue(element);
            while(!tempQ.isEmpty()){
                q.enqueue(tempQ.dequeue());
            }
        }

Here's the driver function:-这是司机 function:-

Output expected: 1 2 3 4 5 Output 预计:1 2 3 4 5

Actual Output: 1 2 1 2 1实际 Output:1 2 1 2 1

int main(){
    int arr[] = {1,2,3,4,5};
    StackUsingTwoQueues s;

    for(int i = 0; i < 5; i++){
        s.push(arr[i]);
        cout<<s.top()<<" ";
    }cout<<endl;

}

The conditions for this tasks are given.给出了此任务的条件。 2 queues shall be used.应使用 2 个队列。

The problem is that you use a variable value as the end condition in your for loops.问题是您在for循环中使用变量值作为结束条件。 You use the size() function, with the initial correct value.您使用size() function,初始值正确。 But then you are popping elements from the queue and then size will be one less.但是随后您从队列中弹出元素,然后size将减少一个。

That can logically not work.这在逻辑上是行不通的。

Instead of this: Please assign the size() to a different variable, just before the loop.而不是这样:请将size()分配给另一个变量,就在循环之前。 And then use this loop invariant value as the end-condition for the loop.然后使用这个循环不变值作为循环的结束条件。 Do this for both loops.对两个循环执行此操作。

The rest of your function is perfectly fine.您的 function 的 rest 完全没问题。

Please see below your corrected code:请在下面查看您更正后的代码:

#include <iostream>
#include <list>

template <class T>
class QueueUsingLL {
    std::list<T> data{};
public:
    bool isEmpty() const { return data.empty(); }
    std::size_t size() const { return data.size(); }
    void enqueue(const T& value) { data.push_back(value); }
    T dequeue() { T value{ data.front() }; data.pop_front(); return value; }
    T front() { return data.front(); };
};


class StackUsingTwoQueues {
    QueueUsingLL<int> q;
    QueueUsingLL<int> tempQ;

public:
    bool isEmpty() {
        return q.isEmpty();
    }
    int pop() {
        if (isEmpty()) {
            return -1;
        }
        else {
            return q.dequeue();
        }
    }
    int size() {
        return q.size();
    }

    // following push method is problematic : -

    void push(int element) {
        if (q.isEmpty()) {
            q.enqueue(element);
        }
        else {
            std::size_t numberOfElements{ q.size() };
            for (std::size_t i = 0; i < numberOfElements; i++) {
                tempQ.enqueue(q.dequeue());
            }
            q.enqueue(element);

            numberOfElements =  tempQ.size() ;
            for (int i = 0; i < numberOfElements; i++) {
                q.enqueue(tempQ.dequeue());
            }
        }
    }
    int top() {
        return q.front();
    }
};
int main() {
    int arr[] = { 1,2,3,4,5 };
    StackUsingTwoQueues s;

    for (int i = 0; i < 5; i++) {
        s.push(arr[i]);
        std::cout << s.top() << " ";
    }
    std::cout << '\n';
    while (s.size())
        std::cout << s.pop() << ' ';
}

I made a stub class for your queue.我为您的队列创建了一个存根 class。

暂无
暂无

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

相关问题 试图摆脱我的堆栈推送功能中的重复索引。 我正在使用2D数组模拟链接列表 - Trying to get rid of repeated indexes in my stack push function. I am using a 2d array to simulate a linked list 尽管对我来说一切都很好,但为什么我的一段代码表现得很奇怪 - Why is my piece of code behaving weird although when everything seems fine to me 我正在尝试使用简单的堆栈推送和弹出来反转字符串。 但是我收到了一些我无法理解的错误 - I am trying to reverse a string using simple stack push and pop. However I am receiving some error that I am not able to make sense of c++ - 如何确保多线程分别在两个无锁队列上推送两个值(在一个原子操作中)而不使用 C++ 中的锁? - How to ensure multi-threads push two values on two lockfree queues respectively (in one atomic operation) without using lock in C++? 在此堆栈操作中我哪里出错了? - Where I am going wrong in this stack operation? 使用2个队列实现堆栈 - Implementing Stack using 2 Queues 有问题的代码??? 我的析构函数有问题吗? - Problematic code??? Is there a problem with my destructor? 我正在尝试使用 cpp 中的链表实现二叉树,但我的代码不起作用 - I'm trying to implement Binary tree using linked list in cpp, But my code is not working 我的代码看起来不错,但某些输入会导致不需要的输出 - My code seems fine, but certain inputs result in undesired outputs 使用消息队列进行堆栈粉碎 - Stack Smashing Using Message Queues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM