请帮忙看一个函数!
学习数据结构写了一个带头节点的双向链表,中间节点和头节点的定义如下:
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;
}