数据结构实验-链表-free函数陷阱

019-1-30
debug是让人头疼的事

数据结构实验-链表-free函数陷阱
在练习中我成功的又崩溃了一次;通过断点调试

status DestroyList(Linklist  head) {//⑵销毁表
	Linklist L = head, Lp;
	while (L->next != NULL) {
		L = Lp;
		L = L->next;
		free(Lp);
	}
	free(head);
	return OK;

发现free(Lp);会卡出,经过查找资料,调试。
发现应该在malloc函数使用时出错;

typedef struct node {  //链表(顺序结构)的定义
	ElemType  elem;
	struct node * next;
}Node ,*Linklist ;

	head = (Linklist)malloc(sizeof(Linklist));

sizeof(指针)在使用时正确但在free时出现错误,应该为

	head = (Linklist)malloc(sizeof(Node));