C语言 关于删除链表最后一个结点出错 整理一下代码再发一次
以下是一个系统中的一个删除链表的函数,该函数有一个BUG就是在删除最后一个结点的时候会出错导致程序直接退出。
欢迎各位高手帮忙看看。如果各位知道怎么改,希望各位高手能写个驱动自己测试一下 谢谢。
/*Delete a note of student list*/intdelete_student(student_list_t* listp,char target[]){ student_t* to_freep; student_t* cur_nodep; int is_deleted; if(listp->size==0)/*If the list is empty,exit.*/ is_deleted=0; else if(strcmp(listp->headp->student[0],target)==0){/*If the headp is the target,delete.*/ to_freep=listp->headp; listp->headp=to_freep->restp; free(to_freep); --(listp->size); is_deleted=1; }else{ for(cur_nodep=listp->headp; cur_nodep->restp!=NULL; cur_nodep=cur_nodep->restp){ /*Current note's next note is the target and current note is not the last note.*/ if(cur_nodep->restp!=NULL && strcmp(cur_nodep->restp->student[0],target)==0){ to_freep=cur_nodep->restp; cur_nodep->restp=to_freep->restp; free(to_freep); --(listp->size); is_deleted=1; }else is_deleted=0; } } return(is_deleted);}