一、单链表的销毁
先来初始化一个链表
//单链表的初始化
typedef struct
{
char num[80];
char no[30];
int score;
}ElemType;
typedef struct Londe
{
ElemType data; //数据域
struct Londe *next; //指针域
}Londe , *LinkList;
Status InitList_L(LinkList &L)
{
L = (LinkList) malloc ( sizeof(Londe) );
L -> next = NULL;
return OK;
}
算法的思想:用p指向头结点,然后逐个销毁。
?
算法实现:
Status DestoryList_L(LinkList &L)
{
Londe *p;
while(L)
{
p = L;
L = L -> next;
free(p);
}
return OK;
}
二、单链表的清空
算法思想:依次释放所有节点,并将头结点指向NULL
?算法执行:
Status ClearList(LinkList &L)
{
Londe *p , *q;
p = L -> next;
while(p)
{
p = q;
q = p -> next;
delete p;
}
L -> next = NULL;
return OK;
}
|