头文件:
1 #ifndef LINKLIST_H 2 #define LINKLIST_H 3 4 #include5 #include 6 7 //链表结点 8 typedef struct LINKNODE { 9 int data; 10 struct LINKNODE* next;11 }LinkNode;12 13 14 typedef struct LINKLIST {15 LinkNode* head;16 int size;17 }LinkList;18 19 typedef void(*PRINTLINKNODE)(void*);20 21 22 LinkList* Init_LinkList();23 24 void Insert_LinkList(LinkList* list, int pos, int data);25 26 void RemoveByPos_LinkList(LinkList* list, int pos);27 28 int Size_LinkList(LinkList* list);29 30 int Find_LinkList(LinkList* list, int data);31 32 void Print_LinkList(LinkList* list, PRINTLINKNODE print);33 34 void FreeSpace_LinkList(LinkList* list);35 36 37 #endif
链表实现:
1 #include "linklist.h" 2 LinkList* Init_LinkList() 3 { 4 LinkList* list = malloc(sizeof(LinkList)); 5 list->size = 0; 6 list->head = malloc(sizeof(LinkNode)); 7 list->head->next = NULL; 8 list->head->data = NULL; 9 list->size = 0; 10 } 11 12 void Insert_LinkList(LinkList* list, int pos, int data) 13 { 14 if (list == NULL && (pos < 0 || pos >= list->size)) 15 { 16 printf("输入的信息有误\n"); 17 return; 18 } 19 LinkNode *node = malloc(sizeof(LinkNode)); 20 node->next = NULL; 21 node->data = data; 22 LinkNode* PNode = list->head; 23 for (int i = 0; i < pos; i++) 24 { 25 PNode = PNode->next; 26 } 27 node->next = PNode->next; 28 PNode->next = node; 29 list->size++; 30 } 31 32 void RemoveByPos_LinkList(LinkList* list, int pos) 33 { 34 if (list == NULL || (pos< 0 || pos > list->size - 1)) 35 { 36 printf("参数错误......\n"); 37 return; 38 } 39 LinkNode* Pnode = list->head; 40 for (int i = 0; i < pos; i++) 41 { 42 43 Pnode = Pnode->next; 44 } 45 LinkNode *tempNode = Pnode->next; 46 Pnode->next = Pnode->next->next; 47 printf("删除的元素值是:%d\n", tempNode->data); 48 tempNode->next = NULL; 49 free(tempNode); 50 list->size--; 51 52 } 53 54 int Size_LinkList(LinkList* list) { 55 if (list == NULL) 56 { 57 printf("参数错误\n"); 58 return -1; 59 } 60 return list->size; 61 } 62 63 int Find_LinkList(LinkList* list, int data) 64 { 65 if (list == NULL) 66 { 67 printf("参数出错...\n"); 68 return -1; 69 } 70 LinkNode* pCurrent = list->head->next; 71 int i = 0; 72 while (pCurrent != NULL) { 73 if (pCurrent->data == data) { 74 break; 75 } 76 i++; 77 pCurrent = pCurrent->next; 78 } 79 80 return i; 81 } 82 83 84 void Print_LinkList(LinkList* list, PRINTLINKNODE print) 85 { 86 if (list == NULL) 87 { 88 printf("参数错误...\n"); 89 return; 90 } 91 LinkNode* PNode = list->head->next; 92 for (;PNode != NULL;) 93 { 94 print(PNode); 95 PNode = PNode->next; 96 } 97 } 98 99 void FreeSpace_LinkList(LinkList* list)100 {101 if (list == NULL)102 {103 return;104 }105 LinkNode* PNode = list->head->next;106 LinkNode* deleNode;107 for (; PNode != NULL; )108 {109 deleNode = PNode->next;110 free(PNode);111 PNode = deleNode;112 }113 list->size = 0;114 free(list);115 }
测试程序:
1 #include "linklist.h" 2 3 4 void PrintNode(LinkNode* node) 5 { 6 int data = node->data; 7 printf("%d\n", data); 8 } 9 10 11 void InitData(LinkList* list)12 {13 14 Insert_LinkList(list, 0, 10);15 Insert_LinkList(list, 0, 20);16 Insert_LinkList(list, 0, 30);17 Insert_LinkList(list, 0, 40);18 Insert_LinkList(list, 0, 50);19 Insert_LinkList(list, 0, 10);20 Insert_LinkList(list, 0, 20);21 Insert_LinkList(list, 0, 30);22 Insert_LinkList(list, 0, 40);23 Insert_LinkList(list, 0, 50);24 Insert_LinkList(list, 0, 10);25 }26 // 27 void remove_x_Data(LinkList* list, int X)28 {29 if (list == NULL)30 {31 printf("参数为NULL 不合法\n");32 33 }34 LinkNode* PreNode = list->head;35 LinkNode* currentNode = list->head->next; 36 LinkNode* delere_node;37 while (currentNode != NULL)38 {39 if (currentNode->data == X)40 {41 42 PreNode->next = currentNode->next;43 delere_node = currentNode;44 45 free(delere_node);46 list->size--;47 currentNode = PreNode->next;48 }49 else50 {51 PreNode = currentNode;52 currentNode = currentNode->next;53 54 }55 }56 57 }58 59 60 void main(void)61 {62 LinkList* list = Init_LinkList();63 InitData(list);64 printf("删除前\n");65 Print_LinkList(list, PrintNode);66 remove_x_Data(list, 50);67 printf("删除后\n");68 Print_LinkList(list, PrintNode);69 system("pause");70 71 }