在C++中实现双向链表时遇到调试问题

Facing debugging problem when implementing doubly linked list in C++

我正在实现一个双向链表,其中每个节点都有两个指针。一个指向列表中的下一个节点,而另一个指向前一个节点。 节点结构由一个整数和一个指向列表中下一个节点的节点指针组成。另一个指针指向列表中的前一个指针。 class 包含两个节点指针:一个指向列表的头部,一个指向列表的尾部。如果列表为空,则它们都应指向 nullptr。

我的密码是

#include <iostream>
using namespace std;

struct Node
{
    int value;
    Node *next;
    Node *tail; //previous node pointer
};

class LinkedList
{
private:
    Node *head;
    Node *tail;

public:
    int size;
    LinkedList()
    {
        head = nullptr;
        tail = nullptr;
        size = 0;
    }

    int length()
    {
        return size;
    }

    void append(int val)
    {
        if (head == nullptr)
        {
            head = new Node(val);
            return;
        }

        // Iterate to end of list
        Node *current;
        current = head;
        while (current->next != nullptr)
        {
            current = current->next;
        }

        // Link new node to end of list
        current->next = new Node(val);
    }
};

int main()
{

};

我收到这个错误:

error: no matching constructor for initialization of 'Node'
            head = new Node(val);
                       ^    ~~~
linked_list.cpp:4:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const Node' for 1st
      argument
struct Node
       ^
linked_list.cpp:4:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
2 errors generated.

欢迎任何advice/links关于我可以在哪里阅读更多关于这个主题的信息:)提前谢谢你!

要调用 new Node(val),其中 val 是一个 int,您的 Node 需要一个以 int 作为参数的构造函数。

也许:

struct Node
{
    int value;
    Node *next;
    Node *tail; 

    Node(int v) : value(v), next(nullptr), tail(nullptr) { }
};