【问题标题】:C++ Why I make new struct and get error ? (Double linked list circular)C++ 为什么我创建新结构并得到错误? (双向链表循环)
【发布时间】:2017-03-30 23:19:09
【问题描述】:

首先,我将双链表循环成这样:

1 3 9 11 13

在那之后,我想在 3 之后插入 99。所以我必须像这样创建一个变量结构:

node *baru;
baru = new node;

但事实停在那里。 这是我的完整代码:

#include <iostream>

using namespace std;

struct node
{
    int item;
    node *next;
    node *prev;
};
int main()
{
    node* list = new node;

    list->item = 1;
    list->next->item = 3;
    list->next->next->item = 9;
    list->next->next->next->item = 11;
    list->next->next->next->next->item = 13;
    list->next->next->next->next->next = list;
    list->prev = list->next->next->next->next;
    list->next->prev = list;
    list->next->next->prev = list->next;
    list->next->next->next->prev = list->next->next;
    list->next->next->next->next->prev = list->next->next->next;
    node *bantu = list;

    for (int i = 1; i <= 10; i++)
    {
        cout << bantu->item << endl;
        bantu = bantu->next;
    }

    cout << endl;

    bantu = list;
    for (int i = 1; i <= 10; i++)
    {
        cout << bantu->item << endl;
        bantu = bantu->prev;
    }

    node *baru;
    baru = new node;

    cout << "go !" << endl;

    baru->item = 99;

    baru->next = list->next->next;
    baru->prev = list->next;
    list->next->next = baru;
    list->next->next->next->prev = baru;

    cout << endl;
    bantu = list;
    for (int i = 1; i <= 10; i++)
    {
        cout << bantu->item << endl;
        bantu = bantu->next;
    }

    cout << endl;
    bantu = list;
    for (int i = 1; i <= 10; i++)
    {
        cout << bantu->item << endl;
        bantu = bantu->prev;
    }
    cout << "SELESAI" << endl;
    return 0;
}

如果成功,程序肯定会输出“go”。但在这种情况下,程序不会输出“go”。这意味着创建新结构时出错。但是为什么会报错呢?

抱歉英语不好 :D 谢谢你

【问题讨论】:

  • 强烈建议花一些时间在 insert 函数上
  • list-&gt;next 未初始化,这是未定义的行为。它指向的地方没有node

标签: c++ pointers struct linked-list runtime-error


【解决方案1】:
node *next;

next 声明为指向node 的指针。滥用一句老话,“指针会指向。”但它并没有指出没有帮助的任何有用的东西。

node* list = new node;

创建一个名为listnode 指针并将其指向一个全新的node。没有创建额外的nodes,因此listnext 无处可指。这意味着

list->next->item = 3;

进入undefined behaviour 尝试访问list-&gt;next 以获取item

解决方案是创建一个insert 函数来创建一个新的node 并执行链接。

node * insertAfter(node * top, int value)
{
    node * temp = new node;
    temp->item = value;
    temp->next = nullptr; // so we know there is nothing next. 
                          // But what if there is something next? Hmmm....
    temp->prev = top;     // point back at top
    top->next = temp;     // point at temp, but what if next already pointed at something?
                          // Wowzers! This is harder than it looks!
    return temp; // return the new node so caller can do something with it if they want
}

【讨论】:

    【解决方案2】:

    node* next; 指针永远不会被初始化,并且不会指向节点的任何实例。尝试在不存在的对象中设置变量是没有意义的,并且会引发错误。为了使其工作,您必须将next 设置为指向一个节点您尝试分配next-&gt;item 一个值之前。

    正如有人已经提到的那样,您确实应该考虑实现插入功能。如果你想插入最后一个节点(根据你的代码你似乎想要),这样的东西应该可以工作:

    void insert(node* root, int value)
    {
        node* temp = root;
        while(temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = new node(value);
        temp->next->prev = temp;
    }
    

    您还应该将节点结构更改为:

    struct node
    {
        node(int item)
        {
            this->item = item;
        }
        int item;
        node *next = nullptr;
        node *prev = nullptr;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      相关资源
      最近更新 更多