14、企业链表
LinkList.h
1 #ifndef LINKLIST_H 2 #define LINKLIST_H 3 4 #include<stdio.h> 5 #include<stdlib.h> 6 #include<string.h> 7 8 9 //链表小结点 10 typedef struct LINKNODE { 11 struct LINKNODE* next; 12 }LinkNode; 13 14 //链表结点 15 typedef struct LINKLIST { 16 LINKNODE head; 17 int size; 18 }LinkList; 19 //遍历函数指指针 20 typedef void(*PRINTNODE)(LinkNode*); 21 //比较函数指针 22 typedef int(*COMPARENODE)(LinkNode*, LinkNode*); 23 24 //初始化链表 25 LinkList* Init_LinkList(); 26 //插入 27 void Insert_LinkList(LinkList* list, int pos, LinkNode* data); 28 //删除 29 void Remove_LinkList(LinkList* list, int pos); 30 //查找 31 int Find_LinkList(LinkList* list, LinkNode* data,COMPARENODE compare); 32 //返回链表大小 33 int Size_LinkList(LinkList* list); 34 //打印 35 void Print_LinkList(LinkList* list, PRINTNODE print); 36 //释放链表内存 37 void FreeSpace_LinkList(LinkList* list); 38 39 40 41 42 #endif
LinkList.c
1 #include"LinkList.h" 2 3 //初始化链表 4 LinkList* Init_LinkList() { 5 LinkList* list = (LinkList*)malloc(sizeof(LinkList)); 6 list->head.next = NULL; 7 list->size = 0; 8 return list; 9 } 10 //插入 11 void Insert_LinkList(LinkList* list, int pos, LinkNode* data) { 12 13 if (list == NULL) 14 { 15 return; 16 } 17 if (data == NULL) { 18 return; 19 } 20 if (pos < 0 || pos >= list->size) { 21 pos = list->size; 22 } 23 //查找插入位置 24 LinkNode* pCurrent = &(list->head); 25 for (int i = 0; i < pos; i++) { 26 pCurrent = pCurrent->next; 27 } 28 //插入新结点 29 data->next = pCurrent->next; 30 pCurrent->next = data; 31 32 list->size++; 33 } 34 //删除 35 void Remove_LinkList(LinkList* list, int pos) { 36 if (list == NULL) 37 { 38 return; 39 } 40 if (pos < 0 || pos >= list->size) { 41 return; 42 } 43 //辅助指针变量 44 LinkNode* pCurrent = &(list->head); 45 for (int i = 0; i < pos; i++) { 46 pCurrent = pCurrent->next; 47 } 48 //删除结点 49 pCurrent->next = pCurrent->next->next; 50 51 list->size--; 52 53 } 54 //查找 55 int Find_LinkList(LinkList* list, LinkNode* data, COMPARENODE compare) { 56 if (list == NULL) 57 { 58 return -1; 59 } 60 if (data == NULL) { 61 return -1; 62 } 63 //辅助指针变量 64 LinkNode* pCurrent = list->head.next; 65 int index = 0; 66 int flag = -1; 67 while (pCurrent != NULL) { 68 if(compare(pCurrent,data)==0) 69 { 70 flag = index; 71 break; 72 } 73 74 pCurrent = pCurrent->next; 75 index++; 76 } 77 return flag; 78 } 79 //返回链表大小 80 int Size_LinkList(LinkList* list) { 81 return 0; 82 } 83 //打印 84 void Print_LinkList(LinkList* list, PRINTNODE print) { 85 if (list == NULL) { 86 return; 87 } 88 //辅助指针变量 89 LinkNode* pCurrent = list->head.next; 90 while (pCurrent != NULL) { 91 print(pCurrent); 92 pCurrent = pCurrent->next; 93 } 94 } 95 //释放链表内存 96 void FreeSpace_LinkList(LinkList* list) { 97 if (list == NULL) { 98 return; 99 } 100 free(list); 101 }
main.c
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 #include"LinkList.h" 6 7 typedef struct PERSON { 8 LinkNode node; 9 char name[64]; 10 int age; 11 }Person; 12 13 void MyPrint(LinkNode* data) { 14 Person* p = (Person*)data; 15 printf("Name:%s Age:%d\n",p->name,p->age); 16 } 17 18 int MyCompare(LinkNode* nodel1, LinkNode* nodel2) { 19 Person* p1 = (Person*)nodel1; 20 Person* p2 = (Person*)nodel2; 21 if (strcmp(p1->name,p2->name)==0 && p1->age==p2->age) { 22 return 0; 23 } 24 return -1; 25 } 26 27 28 int main() 29 { 30 LinkList* list = Init_LinkList(); 31 32 //创建数据 33 Person p1, p2, p3, p4, p5; 34 strcpy(p1.name, "aaa"); 35 strcpy(p2.name, "bbb"); 36 strcpy(p3.name, "ccc"); 37 strcpy(p4.name, "ddd"); 38 strcpy(p5.name, "eee"); 39 40 p1.age = 10; 41 p2.age = 20; 42 p3.age = 30; 43 p4.age = 40; 44 p5.age = 50; 45 46 47 //将结点插入到链表 48 Insert_LinkList(list, 0, (LinkNode*)&p1); 49 Insert_LinkList(list, 0, (LinkNode*)&p2); 50 Insert_LinkList(list, 0, (LinkNode*)&p3); 51 Insert_LinkList(list, 0, (LinkNode*)&p4); 52 Insert_LinkList(list, 0, (LinkNode*)&p5); 53 54 //打印 55 Print_LinkList(list, MyPrint); 56 57 //删除结点 58 Remove_LinkList(list, 2); 59 60 //打印 61 printf("------------------\n"); 62 Print_LinkList(list, MyPrint); 63 64 //查找 65 Person findP; 66 strcpy(findP.name, "bbb"); 67 findP.age = 20; 68 int pos=Find_LinkList(list, (LinkNode*)&findP, MyCompare); 69 printf("位置:%d\n",pos); 70 71 //释放链表内存 72 FreeSpace_LinkList(list); 73 74 system("pause"); 75 return 0; 76 }
VS2015运行结果: