【问题标题】:C++ Queue with Linked-list带有链表的 C++ 队列
【发布时间】:2021-06-26 06:07:18
【问题描述】:

问题:

struct QueueNode {
    int data;
    QueueNode *next;
};

  • int size() const:返回数据大小。
  • bool is_empty() 常量:
  • void enqueue(int val):将新节点添加到列表末尾。
  • void dequeue():移除head指向的节点。
  • int top() const:返回下一个出队的数据。

这是我的代码

class Queue {
    private:
        QueueNode *_head = NULL, *_tail = NULL;
        int _size = 0;
    public:
        int size() const {
            if (! is_empty())
              return _size;
        }
        bool is_empty() const {
            if (_size == 0)
                return true;
            else
                return false;
        }
        void enqueue(int val) {
            QueueNode *n = new QueueNode;
            n -> data = val;
            n -> next = NULL;
            if (is_empty()) {
                _head = n;
                _tail = n;
                _size ++;
            }
            else {
                _tail -> next = n;
                _tail = n;
                _size ++;
            }
        }
        void dequeue() {
            QueueNode *temp;
            temp = _head;
            if (! is_empty()) {
                _head = _head -> next;
                delete temp;
                _size --;
            }
        }
        int top() const {
            if (! is_empty())
                return _head -> data;
        }
};

在线裁判显示错误答案。 我认为“int top() const”是错误的。 但我不知道。 请求帮忙。 谢谢。

【问题讨论】:

  • 队列为空时size()top()返回什么?
  • _head = _tail = n; 应该写成单独的作业。如果队列为空,top()size() 不返回任何内容。
  • @kaylum 不需要返回,只有返回队列不为空
  • 呃,方法不是这样工作的。如果您将其定义为返回某些内容,则它必须针对每种情况都这样做。否则结果是未定义的行为。
  • @dave _head = n; _tail = n; 没关系。

标签: c++ data-structures linked-list queue


【解决方案1】:

正如@Kaylum 指出的那样,如果队列为空,您还没有返回队列的大小。在这种情况下它应该返回 0,但您的 size() 方法中没有其他部分。

这应该可行:

int size() const {
            if (! is_empty())
              return _size;
            else
              return 0;
        }

编辑:同样,您也应该从top() 返回一些东西。如果您使用在线法官,那么它会指定在空队列的情况下返回什么。请再次阅读约束,我想这会有所帮助。这很可能是在线法官判断空队列的输出时需要的一些异常或一些默认整数。

【讨论】:

  • 为什么不只是return _size; 没有if-else
  • @Evg 正是我的想法,但我试图对 OP 的代码进行最小的更改
【解决方案2】:

我的回答。谢谢大家。

class Queue {
    private:
        QueueNode *_head = NULL, *_tail = NULL;
        int _size = 0;
    public:
        int size() const {
            if (! is_empty())
              return _size;
            else
              return 0;
        }
        bool is_empty() const {
            if (_size == 0)
                return true;
            else
                return false;
        }
        void enqueue(int val) {
            QueueNode *n = new QueueNode;
            n -> data = val;
            n -> next = NULL;
            if (is_empty()) {
                _head = _tail = n;
                _size ++;
            }
            else {
                _tail -> next = n;
                _tail = n;
                _size ++;
            }
        }
        void dequeue() {
            QueueNode *temp;
            temp = _head;
            if (! is_empty()) {
                _head = _head -> next;
                delete temp;
                _size --;
            }
        }
        int top() const {
            if (! is_empty())
                return _head -> data;
        }
};

【讨论】:

  • 1) is_empty() 可以写成return _size == 0;。 2) size() 可能只是 return _size;。 3) 首选nullptr 而不是NULL。 4) top() 还是坏了。 5) 缺少析构函数。 6) ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多