void Print(Node *head)
{
if (head = NULL)
{
cout << "NULL";
}
else
{
cout << head->data << endl;
head = head->next;
while (head->next != NULL)
{
cout << head->data << endl;
head = head->next;
}
cout << "NULL";
}
}
答案 0 :(得分:0)
我看到的一个问题是你为头
分配了NULL if (head = NULL)
必须为if (head == NULL)
答案 1 :(得分:0)
我将假设该行
if ( head = NULL )
在转录代码时出错,而且您的工作代码使用
if ( head == NULL )
我看到的真正错误是你正在使用
cout << head->data << endl;
head = head->next;
while (head->next != NULL) // This line is not good
{
cout << head->data << endl;
head = head->next;
}
cout << "NULL";
该行始终是个问题。在某些时候,head
将等于NULL
,您将尝试访问NULL
指针。
将该代码块更改为:
while (head != NULL)
{
cout << head->data << endl;
head = head->next;
}
cout << "NULL";
实际上,整个功能可以是:
void Print(Node* head)
{
while (head != NULL)
{
cout << head->data << endl;
head = head->next;
}
cout << "NULL";
}