简体   繁体   English

删除列表C中的奇数

[英]Delete odd-numbers in a list C

I need help for list and nodes in C. I defined them as struct as follow in a .h file: 我需要C中列表和节点的帮助。我在.h文件中将它们定义为struct,如下所示:

typedef struct node{

     int value;
     struct node*prox;

 }node;

 typedef Nodo *list;

I can't understand why the function to delete all odd numbers is not working. 我不明白为什么删除所有奇数的功能不起作用。 The program is just crashing. 该程序只是崩溃。 Where am I doing wrong? 我在哪里做错了?

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;

}

do you know how to use a debugger and step through the code while looking at the values of the variables? 您知道如何使用调试器并在查看变量值的同时逐步执行代码吗? In any case, in the large "else" statement, you are initializing prev=NULL, and then further down, you are referencing prev->prox -- so you are dereferencing a null pointer 无论如何,在大的“ else”语句中,您要初始化prev = NULL,然后进一步向下,您要引用prev-> prox,因此要取消引用空指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM