首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言 关于删除链表最后一个结点出错 整理一下代码再发一次,该如何处理

2012-04-09 
C语言 关于删除链表最后一个结点出错 整理一下代码再发一次以下是一个系统中的一个删除链表的函数,该函数

C语言 关于删除链表最后一个结点出错 整理一下代码再发一次
以下是一个系统中的一个删除链表的函数,该函数有一个BUG就是在删除最后一个结点的时候会出错导致程序直接退出。
欢迎各位高手帮忙看看。如果各位知道怎么改,希望各位高手能写个驱动自己测试一下 谢谢。

C/C++ code
/*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);}


[解决办法]
其实这个地方还要看你的链表listp最后的节点是不是NULL,要赋值为NULL应该就好了,这个我们都没怎么有办法测试,但是和基本的单链表操作是一样的

热点排行