我的C++代码C++:分割故障
#include <iostream>
#include <cstdlib>
using namespace std ;
struct Node {
int data ;
struct Node* next;
};
Node* Insert(Node *head,int data);
int main(){
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 7;
head->next = second;
second->data = 17;
second->next = third;
third->data = 37;
third->next = NULL;
head = Insert(head,3);
}
Node* Insert(Node *head,int data)
{
while (true)
{ cout << "hey" ;
if (head == NULL)
{
cout << "hey";
head->data = data ;
head->next = NULL ;
break ;
}
head = head->next ;
}
return head;
}
我特林学习链表在C++中。 我正在通过一个头和数据插入节点使用我已经定义的Insert()
函数。
我的输出端子
Segmentation fault (core dumped)
我觉得我在一个错误的方式invocating Insert()
功能。 请帮忙! 谢谢!
调试它一步一个调试步骤,你就会知道什么错。 –
不要在C++代码中使用malloc。使用新的操作符。不要使用“using namespace std”并学会使用调试器而不是将print语句放在任何地方。 – LukeG
我试着在主函数下面的第一行添加'cout'。 它没有打印任何东西。 问题出在运行时iself –