我正在用C++编写一个函数来将“int”类型的“数据”添加到链表的末尾。C++中链接列表实现中的分段错误
void insert_back()
{
int no;
node *temp;
cout<<"\nEnter the number"<<"\n";
cin>>no;
temp = head;
if(temp != NULL)
{
while(temp != NULL)
temp = temp->next;
}
temp->next = (node*)malloc(sizeof(node));
temp = temp->next;
temp->data = no;
temp->next = NULL;
}
然而,在该行,TEMP->下一=(节点*)malloc的(的sizeof(节点)),我得到访问冲突错误(分段错误)。我没有发现任何根本错误。你可以在这个问题上给我启发吗?
它不进入循环。而是一个新的节点被初始化,它被temp指向。我的主要链表是“list”,所以实际上我需要给出一个声明:list = temp;在函数结束时。 –