简体   繁体   English

C++ 队列与链表

[英]C++ Queue with Linked-list

Question:问题:

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

do

  • int size() const: return data size. int size() const:返回数据大小。
  • bool is_empty() const: bool is_empty() 常量:
  • void enqueue(int val): add a new node to the end of the list. void enqueue(int val):在列表末尾添加一个新节点。
  • void dequeue(): remove the node which head point to. void dequeue():移除 head 指向的节点。
  • int top() const: return the data which will be dequeue next. int top() const:返回下一个出队的数据。

This is my code这是我的代码

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

The Online Judge displayed wrong answer.在线法官显示错误答案。 I think the "int top() const" is wrong.我认为“int top() const”是错误的。 But I have no idea.但我不知道。 Ask for help.请求帮忙。 Thanks.谢谢。

As @Kaylum pointed out, you have not returned the size of the queue if it is empty.正如@Kaylum 指出的那样,如果队列为空,您还没有返回队列的大小。 It should return 0 in that case, but you have no else part in your size() method.在这种情况下它应该返回 0,但你的size()方法中没有其他部分。

This should probably work:这应该可以工作:

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

EDIT: Similarly you should return something from top() too.编辑:同样你也应该从top()返回一些东西。 If you're using an online judge, then it would have specified what to return in case of empty queues.如果您使用在线法官,那么它会指定在空队列的情况下返回什么。 Read the constraints again please, I guess it would help.请再次阅读约束,我想这会有所帮助。 It would most probably some exception or some default integer which would be required by the online judge to judge an empty queue's output.这很可能是一些异常或一些默认的 integer,在线法官需要这些异常来判断一个空队列的 output。

My answer.我的答案。 Thanks Everyone.感谢大家。

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