連結串列的清空操作,就是要銷燬掉除表頭結點之外的所有結點。只要寫個迴圈來銷燬結點就可以,注意,要先記錄當前結點的後繼結點,然後再銷燬當前結點。
參考程式碼:
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct SqList{
ElemType elem;
struct SqList *next;
}SqList;
int ClearList(SqList *list)
{
SqList *temp, *head = list;
if (list == NULL)
return ERROR;
list = list->next;
while (list != NULL)
temp = list->next;
free(list);
list = temp;
}
head->next = NULL;
return OK;
連結串列的清空操作,就是要銷燬掉除表頭結點之外的所有結點。只要寫個迴圈來銷燬結點就可以,注意,要先記錄當前結點的後繼結點,然後再銷燬當前結點。
參考程式碼:
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct SqList{
ElemType elem;
struct SqList *next;
}SqList;
int ClearList(SqList *list)
{
SqList *temp, *head = list;
if (list == NULL)
return ERROR;
list = list->next;
while (list != NULL)
{
temp = list->next;
free(list);
list = temp;
}
head->next = NULL;
return OK;
}