首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-02-15 
大家帮我看看这个函数哪里错了,是删除链表第一个结点的函数注意:head指向是头节点,是在链表第一个结点前面

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

注意: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)--; 
}

热点排行