代码非常简单,不多少,list.h
1 #ifndef list_h 2 #define list_h 3 4 #include <stdlib.h> 5 #include <stdio.h> 6 7 #define MaxSize 10 8 9 typedef int DataType; 10 11 typedef struct _node{ 12 /*int id; 13 bool sex; 14 char name[MaxSize];*/ 15 DataType data; 16 17 struct _node *next; 18 } Node; 19 20 typedef struct _list{ 21 Node *head; 22 Node *tail; 23 Node *current; 24 } List; 25 26 void initList(List *); 27 void addHead(List *, DataType); 28 void addTail(List *, DataType); 29 void delElem(List *, DataType); 30 void modiElem(List *, DataType, DataType); 31 int locaElem(List *, DataType); 32 int lenthList(List *); 33 void travList(List *); 34 35 #endif
实现list.c
1 #include "list.h" 2 3 void initList(List *list){ 4 printf("begin initList(): "); 5 list->head = NULL; 6 list->tail = NULL; 7 list->current = NULL; 8 9 return; 10 } 11 12 void addHead(List *list, DataType data){ 13 printf("begin addHead(): "); 14 //申请新节点的空间 15 Node *node = (Node *)malloc(sizeof(Node)); 16 //修正新节点的数据域 17 node->data = data; 18 //更该新节点的指针域 19 node->next = NULL; 20 21 if(list->head == NULL){ 22 list->tail = node; 23 }else{ 24 node->next = list->head; 25 } 26 list->head = node; 27 28 return; 29 } 30 31 void addTail(List *list, DataType data){ 32 printf("begin addTail(): "); 33 //申请新节点的空间 34 Node *node = (Node *)malloc(sizeof(Node)); 35 //修正新节点的数据域 36 node->data = data; 37 //更该新节点的指针域 38 node->next = NULL; 39 40 if(list->head == NULL){ 41 list->head = node; 42 }else{ 43 list->tail->next = node; 44 } 45 list->tail = node; 46 47 return; 48 } 49 50 void delElem(List *list, DataType data){ 51 printf("begin delElem(): "); 52 Node *tmp = list->head; 53 if(list->head->data == data){ 54 list->head = list->head->next; 55 } 56 while(tmp && tmp->next){ 57 if(tmp->next->data == data){ 58 tmp->next = tmp->next->next; 59 } 60 tmp = tmp->next; 61 } 62 63 return; 64 } 65 66 void modiElem(List *list, DataType dataB, DataType dataA){ 67 printf("begin modiElem(): "); 68 Node *tmp = list->head; 69 while(tmp->next){ 70 if(tmp->data = dataB){ 71 tmp->data = dataA; 72 } 73 tmp = tmp->next; 74 } 75 76 return; 77 } 78 79 int locaElem(List *list, DataType data){ 80 printf("begin locaElem(): "); 81 Node *tmp = list->head; 82 int i = -1; 83 while(tmp){ 84 if(tmp->data != data){ 85 i++; 86 } 87 tmp = tmp->next; 88 } 89 90 return i; 91 } 92 93 int lenthList(List *list){ 94 printf("begin lenthList(): "); 95 Node *tmp = list->head; 96 int i = 0; 97 while(tmp){ 98 i++; 99 tmp = tmp->next; 100 } 101 102 return i; 103 } 104 105 void travList(List *list){ 106 printf("begin travList(): "); 107 Node *tmp = list->head; 108 int i = 1; 109 while(tmp){ 110 printf("The %dth: %d ", i, tmp->data); 111 tmp = tmp->next; 112 i++; 113 } 114 printf(" "); 115 116 return; 117 }
测试文件testList.c
1 #include "list.h" 2 #define Size 5 3 4 int main(int argc, char **argv) 5 { 6 List *list = (List *)malloc(sizeof(List)); 7 initList(list); 8 for(int i = 0; i < Size; i++){ 9 addHead(list, i*5); 10 } 11 travList(list); 12 printf("the list size: %d ", lenthList(list)); 13 delElem(list, 5); 14 travList(list); 15 printf("the list size: %d ", lenthList(list)); 16 17 List *list2 = (List *)malloc(sizeof(List)); 18 initList(list2); 19 for(int i = 0; i < Size; i++){ 20 addTail(list2, i*3); 21 } 22 travList(list2); 23 printf("the list2 size: %d ", lenthList(list2)); 24 printf("the %d is local: %d ", 6, locaElem(list2, 6)); 25 delElem(list2, 6); 26 travList(list2); 27 printf("the list2 size: %d ", lenthList(list2)); 28 29 return 0; 30 }
Makefile文件:
1 testList:testList.c list.c 2 gcc -g $^ -o $@ 3 clean: 4 rm *.o testList 5 .PHONY: clean
编译:
make
测试
./testList
测试结果:
gcc -g testList.c list.c -o testList begin initList(): begin addHead(): begin addHead(): begin addHead(): begin addHead(): begin addHead(): begin travList(): The 1th: 20 The 2th: 15 The 3th: 10 The 4th: 5 The 5th: 0 begin lenthList(): the list size: 5 begin delElem(): begin travList(): The 1th: 20 The 2th: 15 The 3th: 10 The 4th: 0 begin lenthList(): the list size: 4 begin initList(): begin addTail(): begin addTail(): begin addTail(): begin addTail(): begin addTail(): begin travList(): The 1th: 0 The 2th: 3 The 3th: 6 The 4th: 9 The 5th: 12 begin lenthList(): the list2 size: 5 begin locaElem(): the 6 is local: 3 begin delElem(): begin travList(): The 1th: 0 The 2th: 3 The 3th: 9 The 4th: 12 begin lenthList(): the list2 size: 4