1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct Node { 5 int data; 6 struct Node *next; 7 } Node, *LinkList; 8 9 LinkList initList() { 10 LinkList L = (LinkList *) malloc(sizeof(LinkList)); 11 if (L == NULL) { 12 return -1; 13 } 14 L->next = NULL; 15 return L; 16 } 17 18 int ListEmpty(LinkList L) { 19 if (L->next) 20 return 1; 21 else 22 return 0; 23 } 24 25 void DestroyList(LinkList L) { 26 LinkList P; 27 while (L) { 28 P = L; 29 L = L->next; 30 free(P); 31 } 32 } 33 34 void InsertList(LinkList L, int num) { 35 LinkList pNew = (LinkList *) malloc(sizeof(LinkList)); 36 pNew->next = L->next; 37 pNew->data = num; 38 L->next = pNew; 39 } 40 41 void printList(LinkList L) { 42 LinkList P = L->next; 43 while (P) { 44 printf("%d\n", P->data); 45 P = P->next; 46 } 47 } 48 49 void CleanList(LinkList L) { 50 LinkList P, Q; 51 P = L->next; 52 while (P) { 53 Q = P->next; 54 free(P); 55 P = Q; 56 } 57 L->next = NULL; 58 } 59 60 int main() { 61 LinkList pHead = initList(); 62 if (ListEmpty(pHead)) { 63 printf("表不为空\n"); 64 } else { 65 printf("表空\n"); 66 } 67 for (int i = 0; i < 5; ++i) { 68 InsertList(pHead, i); 69 } 70 printList(pHead); 71 if (ListEmpty(pHead)) { 72 printf("表不为空\n"); 73 } else { 74 printf("表空\n"); 75 } 76 CleanList(pHead); 77 if (ListEmpty(pHead)) { 78 printf("表不为空\n"); 79 } else { 80 printf("表空\n"); 81 } 82 return 0; 83 }