读书人

大家帮小弟我看看这个函数错哪了是删

发布时间: 2012-02-15 12:09:43 作者: rapoo

大家帮我看看这个函数哪里错了,是删除链表第一个结点的函数

注意:head指向是头节点,是在链表第一个结点前面的独立的结点,不是第一个结点,而tail就是指向最后一个结点

一旦调用这个函数,指针似乎就出了问题,感觉好像没有错啊

/*Delete the first node*/
void DelFirst(LinList *pList)
{
if(pList->length==1)
{
free(pList->head->next);
pList->head->next=NULL;
}
else
{
Node *OldNode=pList->head->next;
pList->head->next=pList->head->next->next;
free(OldNode);


}
(pList->length)--;
}

[解决办法]
你的代码似乎每问题,只是必须确保head是一个不用但是必须分配的指针
合理的是我注释中的代码,对比一下吧
/*Delete the first node*/
void DelFirst(LinList *pList)
{
if(pList- >length==1)
{
free(pList- >head- >next); //free(pList- >head)
pList- >head->next=NULL; // pList->head = NULL;
}
else
{
Node *OldNode=pList- >head- >next; //Node *OldNode=pList- >head;
pList- >head- >next=pList- >head- >next- >next; //pList- >head=pList- >head- >next;
free(OldNode);


}
(pList- >length)--;
}

读书人网 >软件架构设计

热点推荐