C++ 为什么我制作新结构并得到错误?(双链表通函)
C++ Why I make new struct and get error ? (Double linked list circular)
首先,我像这样制作双链表循环:
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;
制作一个名为 list
的node
指针,并将其指向一个全新的node
。没有创建额外的node
,所以list
的next
无处可寻。这意味着
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;
};
相关文章:
- 错误:需要类名(链表c++)
- 对单向链表进行排序时出现运行时错误
- 在链表中的第 n 位插入显示分割错误
- 链表错误的中间
- 为什么我在空指针错误(链表)中获取成员访问权限
- 查找显示链表时出现的错误
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 在 c++ 中实现链表时出现分段错误
- 双链表的擦除值函数,未知错误
- 删除链表中的节点 - 分段错误
- 分段错误通用双链表
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- 奇怪的C++链表错误
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 为什么我的C++代码在以下打印链表的代码片段中显示分段错误?
- C++:按数字排序链表错误
- 为什么我在尝试添加两个链表时出现此错误?
- C++ largest_free节点函数段错误链表
- 析构函数分段错误链表
- c++分割错误链表元素添加