gpt4 book ai didi

c - 删除列表C中的奇数

转载 作者:行者123 更新时间:2023-11-30 16:31:54 25 4
gpt4 key购买 nike

我需要 C 中列表和节点的帮助。我将它们定义为 .h 文件中的结构,如下所示:

typedef struct node{

int value;
struct node*prox;

}node;

typedef Nodo *list;

我不明白为什么删除所有奇数的功能不起作用。该程序刚刚崩溃。我哪里做错了?

list delete_odd_numbers(list l){

node*temp;
node*current;
node*prev;

if(l->value%2!=0){

temp=l;
l=l->prox;
free(temp);

}else{

prev=NULL;
current=l;
while(current!=NULL){

if(current!=NULL &&current->value%2!=0){

temp=current;
prev->prox=current->prox;
free(temp);

}else{

prev=current;
current=current->prox;

}
}

}
return l;

}

最佳答案

您知道如何使用调试器并在查看变量值的同时逐步执行代码吗?无论如何,在大的“else”语句中,您正在初始化 prev=NULL,然后再往下,您正在引用 prev->prox——因此您正在取消引用空指针

关于c - 删除列表C中的奇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com