C++ 为什么我制作新结构并得到错误?(双链表通函)

C++ Why I make new struct and get error ? (Double linked list circular)

本文关键字:错误 链表 新结构 为什么 C++ 结构      更新时间:2023-10-16

首先,我像这样制作双链表循环:

1 3 9 11 13

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

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谢谢

node *next;

声明next是指向node的指针。滥用一句老话,"指针指向",但它没有指向任何有用的东西。

node* list = new node;

制作一个名为 listnode指针,并将其指向一个全新的node。没有创建额外的node,所以listnext无处可寻。这意味着

list->next->item = 3;

徘徊在未定义的行为中,试图访问list->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
}

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

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

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