C ++双链表,显示功能错误

时间:2022-06-02 18:52:21
void display()
{
    Node *q;
    q = head;
    if (head == NULL)
    {
        cout << "List empty,nothing to display" << endl;
    }
    else
    {
        cout << "The Doubly Link List is :" << endl;
        while (q->next != NULL)
        {
            cout << "Data stored is " << q->data << " at " << q << endl;
            q = q->next;
        }
        cout << "Data stored is " << q->data << " at " << q << endl;
    }
}

I am having a problem in my display function of Doubly Linked list, like whenever it is called in the main it only runs if part or if I remove the if part the while part to be displayed doesn't work, I have already called insert function which is working good and displayed below.

我在Doubly Linked列表的显示功能中遇到问题,就像在主要调用时它只在部分运行或者如果我删除了if部分,而要显示的while部分不起作用,我已经调用了insert功能良好,显示如下。

void insertAtFirst(int key)
{
    if (head == NULL)
    {
        Node *n = new Node;
        n->data = key;
        n->prev = NULL;
        n->next = NULL;
    }
    else
    {
        Node *temp = new Node;
        temp->data = key;
        temp->prev = NULL;
        temp->next = NULL;
        head->prev = temp;
        head = temp;
    }
}

1 个解决方案

#1


0  

You neither wire up head correctly on an empty-list condition, nor wire up the new node's next on a secondary insertion. As written the secondary insertion would not possibly be executed because head always remains NULL, so catching that error is dependent on (a) code review, (b) interactive debugger, and/or (c) fixing the first problem to begin with.

您既不会在空列表条件下正确连接头,也不会在辅助插入时连接新节点的下一个。由于写入了二次插入,因此头部始终保持为NULL,因此捕获该错误取决于(a)代码检查,(b)交互式调试器,和/或(c)首先解决第一个问题。

Both of these can be fixed while eliminating a decent chunk of code by simply doing this:

通过简单地执行以下操作,可以修复这两个问题,同时消除了相当多的代码:

void insertAtFirst(int key)
{
    Node *temp = new Node;
    temp->data = key;    // save data
    temp->prev = NULL;   // this will be the new head, so no prev
    temp->next = head;   // ok whether this is null or already populated

    // if there's already a head, have its prev point to the new node
    if (head)
        head->prev = temp;

    // regardless, the new head is the new node.
    head = temp;
}

Not as critical, but odd nonetheless, your display function is printing the last line separate from rest of the list. Try:

虽然不是很关键,但奇怪的是,你的显示功能是打印与列表其余部分分开的最后一行。尝试:

void display()
{
    if (head == NULL)
    {
        cout << "List empty; nothing to display" << endl;
    }
    else
    {
        cout << "The Doubly Link List is :" << endl;
        for (const Node *q = head; q; q = q->next)
            cout << "Data stored is " << q->data << " at " << q << endl;
    }
}

#1


0  

You neither wire up head correctly on an empty-list condition, nor wire up the new node's next on a secondary insertion. As written the secondary insertion would not possibly be executed because head always remains NULL, so catching that error is dependent on (a) code review, (b) interactive debugger, and/or (c) fixing the first problem to begin with.

您既不会在空列表条件下正确连接头,也不会在辅助插入时连接新节点的下一个。由于写入了二次插入,因此头部始终保持为NULL,因此捕获该错误取决于(a)代码检查,(b)交互式调试器,和/或(c)首先解决第一个问题。

Both of these can be fixed while eliminating a decent chunk of code by simply doing this:

通过简单地执行以下操作,可以修复这两个问题,同时消除了相当多的代码:

void insertAtFirst(int key)
{
    Node *temp = new Node;
    temp->data = key;    // save data
    temp->prev = NULL;   // this will be the new head, so no prev
    temp->next = head;   // ok whether this is null or already populated

    // if there's already a head, have its prev point to the new node
    if (head)
        head->prev = temp;

    // regardless, the new head is the new node.
    head = temp;
}

Not as critical, but odd nonetheless, your display function is printing the last line separate from rest of the list. Try:

虽然不是很关键,但奇怪的是,你的显示功能是打印与列表其余部分分开的最后一行。尝试:

void display()
{
    if (head == NULL)
    {
        cout << "List empty; nothing to display" << endl;
    }
    else
    {
        cout << "The Doubly Link List is :" << endl;
        for (const Node *q = head; q; q = q->next)
            cout << "Data stored is " << q->data << " at " << q << endl;
    }
}