繁体   English   中英

C++ 队列与链表

[英]C++ Queue with Linked-list

问题:

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”是错误的。 但我不知道。 请求帮忙。 谢谢。

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

这应该可以工作:

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

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

我的答案。 感谢大家。

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;
        }
};

暂无
暂无

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

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