此代码中是否有任何错误?我在这里得到了分段错误。 问题是删除值小于下一个元素的元素。
void Remove()
{
struct Node* prev = NULL;
struct Node* curr = head;
struct Node* Next = NULL;
while(curr!=NULL)
{
Next = curr->next;
if(curr->data < Next->data)
{
if(curr == head)
{
struct Node* temp2 = head;
head = head->next;
free(temp2);
curr = head;
//Next = curr->next;
}
else
{
struct Node* temp1 = curr;
prev->next = curr->next;
free(temp1);
curr = Next;
//Next = curr->next;
}
}
else
{
prev = curr;
curr = Next;
//Next = curr->next;
}
}
}
答案 0 :(得分:1)
这是一个问题:
while(curr!=NULL)
{
Next = curr->next;
if(curr->data < Next->data)
当您到达列表末尾时curr->next
为NULL。所以Next
为NULL。你仍然取消引用它(即Next->data
),所以你的程序(可能)会因seg故障而崩溃。
在访问Next
Next->data
是否为NULL