-1
我的程序的目标是删除具有数字偶数值的所有元素。该程序工作正常,除非它应该删除最后一个元素:这会导致程序崩溃。,C,碰撞,同时尝试删除列表中的最后一个元素
我创建了一个清单,这样的结构:
struct elemento{
int dato;
struct elemento *next;
};
可以忽略如何创建列表,因为我知道,误差仅在这个函数:
struct elemento *eliminapari(struct elemento *p){
struct elemento * start = p;
struct elemento * temp, *temp2;
int cont;
cont = 0;
temp2 = p;
while(p != NULL && cont != 20){
temp2 = temp2->next;
if((p->dato % 2) == 0){
if (cont == 0){ //if first element
start = p->next;
free(p);
p = start;
}else if(p->next == NULL){ //if last element
temp2->next = NULL; //this would be the previous node
free(p);
p = NULL;
}else{
temp = p->next;
p->dato = p->next->dato;
p->next = p->next->next;
free(temp);
}
}else{printf("\n3\n"); p = p->next;}
cont = cont + 1;
}
return(start);
}
感谢。
它在哪里崩溃,它如何崩溃,当时的结构如何,结构如何声明,数据如何分配,你怎么知道它没有被释放?我们不是心灵读者。 –
'temp2-> next = NULL; //这将是前一个节点':'temp2'为'NULL' – BLUEPIXY
你的代码有点混乱,但我的猜测是你正在释放一个NULL指针,尝试释放指针,然后将其设置为NULL –