删除列表C中的奇数

时间:2018-05-16 18:33:33

标签: c list struct

我需要C中列表和节点的帮助。我在.h文件中将它们定义为struct:

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;

}

1 个答案:

答案 0 :(得分:0)

您是否知道如何使用调试器并在查看变量值时逐步执行代码?在任何情况下,在大的“else”语句中,您正在初始化prev = NULL,然后进一步向下,您正在引用prev-> prox - 所以您要取消引用空指针

相关问题