首先,提前感谢所有回复这篇文章的人。
其次,我浏览了所有其他帖子,但找不到任何对我有帮助的东西(抱歉,我是 C++ 新手)。
这是我的代码:
Node* Insert(Node *head,int data) //for linked lists
{
Node* current = head;
while(current -> next != NULL){
current = current -> next;
}
cout << head -> data;
Node *last = new Node();
last -> data = data;
last -> next = NULL;
current -> next = last;
return head;
}
似乎(通过行注释的反复试验)当前指针中下一个属性的访问似乎是问题所在,但我似乎无法弄清楚原因。Node 结构体有两个属性,*next(指向链表中的下一项)和 data(节点的数据)。
有什么想法吗?
linux用户
编辑:问题已解决 - 非常感谢所有留下评论的人!
遗憾的是,我无法使用**pHead
取消引用解决方案,因为问题出在自动输入函数参数的网站上。然而,使用下面的评论,我制作了一个简单的程序,希望能为像我这样的其他初级 C++ 程序员详细说明这个问题:
Node* Insert(Node *head,int data)
{
if(head == NULL){
Node* last = new Node();
last -> data = data;
return last;
}
Node *current = head;
while(current -> next != NULL){
current = current -> next;
}
Node *last = new Node();
last -> data = data;
last -> next = NULL;
current -> next = last;
return head;
}
问候,
linux用户