请帮忙看一个函数!

zhoufanking 2005-08-07 04:49:48
学习数据结构写了一个带头节点的双向链表,中间节点和头节点的定义如下:
typedef struct DLnode
{
Item elem;
struct DLnode *pre;
struct DLnode *next;
DLnode_head *list; // on which list we are
}DLnode;
typedef struct DLnode_head
{
struct DLnode *pre; // point to the previous node
struct DLnode *next; // point to the next node
struct DLnode *tail; // point to the last node of the list
int qlen; // record the len of the list
}DLnode_head;

链表销毁函数如下:
void destory_list(DLnode_head **list)
{

DLnode *start,*next;
start = (*list)->next;
next = start->next;

while( start != NULL )
{


free(start);
start = next;
next = start->next;
}

free(*list);

*list = NULL;
}
这个销毁函数的实现编译通过,但是运行时就出错了,不知道错在哪里但是如果写成下面的样子就对了,我感觉两个是一样的啊,为什么会得到不同的结果呢?
void destory_list(DLnode_head **list)
{

DLnode *start,*next;
start = (*list)->next;

while( start != NULL )
{

next = start->next;
free(start);
start = next;

}

free(*list);

*list = NULL;
}

...全文
177 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Goldrush 2005-08-08
  • 打赏
  • 举报
回复
菜鸟,学习中
pgh 2005-08-08
  • 打赏
  • 举报
回复
这些东西在msdn中很容易查到, 另外我觉得如果free(null)是不会引发异常的.
The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.
pgh 2005-08-08
  • 打赏
  • 举报
回复
问题是这样的,
while( start != NULL )
{


free(start);
start = next;
next = start->next;
}

当start指向链表链表尾部时,大括号里的值即为
free(尾部-1的节点);
start = null;
next = null->next; //error occured
zhoufanking 2005-08-08
  • 打赏
  • 举报
回复
原来如此啊,谢谢 jixingzhong 瞌睡虫,也谢谢其他各位的热心帮忙。瞌睡虫,象free的参数不能为NULL这些东西你都是从哪里知道的啊?什么文档上讲过吗?
jixingzhong 2005-08-08
  • 打赏
  • 举报
回复
while( start != NULL )
{


free(start);
start = next;
next = start->next;
}


关键是 free的特性!!

free 函数要求不能是 NULL 指针!

把你的 条件start != NULL 换成是靠后面的一个点 就可以了

start != NULL ==》 next != NULL

这样就不会处错了

不过 这样的话 前面的程序就要适当的修改一下了 ... ()


void destory_list(DLnode_head **list)
{

DLnode *start,*next;
start = (*list)->next;
next = start->next;

while( next != NULL )
{


free(start);
start = next;
next = start->next;
}

free(start); /*注意不要漏了这里 ! 最后的一个接点是没有释放的!!*/


free(*list);

*list = NULL;
}
zhoufanking 2005-08-07
  • 打赏
  • 举报
回复
每个节点都有一个DLnode_head是为了每个节点都可以方便地找到它的头节点,有时我们得到一个节点的指针,如果我们想知道它是哪个链表上的一个节点,那么这个DLnode_head域就很有用了。其实关于它的优点我也不是很清楚,我参照linux里的那个skbuff结构写的,以后慢慢发现它的优点吧。
llf_hust 2005-08-07
  • 打赏
  • 举报
回复
估计写的是一个十字链表吧
xiaocai0001 2005-08-07
  • 打赏
  • 举报
回复
楼主写的程序让我百思不得其解。
typedef struct DLnode
{
Item elem;
struct DLnode *pre;
struct DLnode *next;
DLnode_head *list; // on which list we are
}DLnode;
为什么链表中每个结点都要有DLnode_head 域呢?
实在想不通!
lbing7 2005-08-07
  • 打赏
  • 举报
回复
free(start);
start = next;
next = start->next;
...........................................
你的这个顺序可能会出现在这样的问题
如果FREE(START);的时候刚刚好是最后一个节点
那么start = next;将会给NULL
next = start->next;从NULL里找NEXT能不出错?

不知道问题是不是在这里;

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧