寫法一:int Empty(List *L)//判空{(diào)return L->size=0;}void Clear(List* L)//清表{while(!Empty(L))Pop_front(L);L->size=0;}void Free(List* L)//撤銷所有節(jié)點空間{Clear(L);free(L->head);free(L->tail);}#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<math.h>typedef int Type;struct Nodestruct Node* next;Type data;struct Node* prev;};typedef struct Node Node;typedef struct{Node* head;int size;Node* tail;}List;int main(){List L;Init(&L);//中間還有對鏈表的一系列操作如前插,前刪等Free(&L);return 0;}寫法二:#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<math.h>typedef int Type;struct Node{struct Node* next;Type data;struct Node* prev;};typedef struct Node Node;typedef struct{Node* head;int size;Node* tail;}List;int main(){List L;Init(&L);//中間還有對鏈表的一系列操作如前插,前刪等free(L.tail);free(L.head);L.head=NULL;L.tail=NULL;L.size=0;return 0;}
程序?qū)懛ㄒ荒睦镉袉栴}? 寫法二是不是就可以釋放所有內(nèi)存了?
慕妹3146593
2022-08-03 19:15:55