LinkList.h
#include <windows.h> #include <stdio.h> // 链表小结点 typedef struct LINKNODE { LINKNODE* next; }LinkNode; // 链表结点 typedef struct LINKLIST { LinkNode head; int size; }LinkList; // 遍历结点的函数指针 typedef void(*PRINTLINKNODE)(LinkNode*); // 比较函数指针 typedef int(*COMPARENODE)(LinkNode*, LinkNode*); // 初始化链表 LinkList* Init_LinkList(); // 根据位置插入语一个结点 void Insert_LinkList(LinkList* list, int pos, LinkNode* data); // 根据位置删除一个结点 void RemoveByPos_LinkList(LinkList* list, int pos); // 查找结点 int Find_LinkList(LinkList* list, LinkNode* data, COMPARENODE compare); // 返回链表的大小 int Size_LinkList(LinkList* list); // 打印链表 void Print_LinkList(LinkList* list, PRINTLINKNODE print); // 释放内存 void FreeMem_LinkList(LinkList* list);
LinkList.cpp
#include "LinkList.h" // 初始化链表 LinkList* Init_LinkList() { LinkList* list = (LinkList*)malloc(sizeof(LinkList)); list->size = 0; list->head.next = NULL; return list; } // 根据位置插入语一个结点 void Insert_LinkList(LinkList* list, int pos, LinkNode* data) { if (list == NULL || data == NULL) { return; } if (pos < 0 || pos >= list->size) { pos = list->size; } LinkNode* pCurrent = &(list->head); for (int i = 0; i < pos; i++) { pCurrent = pCurrent->next; } data->next = pCurrent->next; pCurrent->next = data; list->size++; } // 根据位置删除一个结点 void RemoveByPos_LinkList(LinkList* list, int pos) { if (list == NULL) { return; } if (pos < 0 || pos >= list->size) { return; } LinkNode* pCurrent = &(list->head); for (int i = 0; i < pos; i++) { pCurrent = pCurrent->next; } pCurrent->next = pCurrent->next->next; list->size--; } // 查找结点 int Find_LinkList(LinkList* list, LinkNode* data, COMPARENODE compare) { if (list == NULL || data == NULL) { return -1; } int flag = -1; int index = 0; LinkNode* pCurrent = list->head.next; while (pCurrent != NULL) { if (compare(data, pCurrent) == 0) { flag = index; break; } pCurrent = pCurrent->next; index++; } return flag; } // 返回链表的大小 int Size_LinkList(LinkList* list) { if (list == NULL) { return -1; } return list->size; } // 打印链表 void Print_LinkList(LinkList* list, PRINTLINKNODE print) { if (list == NULL) { return; } LinkNode* pCurrent = list->head.next; while (pCurrent != NULL) { print(pCurrent); pCurrent = pCurrent->next; } } // 释放内存 void FreeMem_LinkList(LinkList* list) { if (list == NULL) { return; } free(list); }
main.cpp
#include "LinkList.h" typedef struct PERSON{ LinkNode node; char name[64]; int age; }Person; void MyPrint(LinkNode* data) { Person* p = (Person*)data; printf("Name:%s, Age:%d\n", p->name, p->age); } int MyCompare(LinkNode* node1, LinkNode* node2) { Person* p1 = (Person*)node1; Person* p2 = (Person*)node2; if ((p1->age == p2->age) && (strcmp(p1->name, p2->name) == 0)) { return 0; } return -1; } int main() { //创建链表 LinkList* list = Init_LinkList(); //创建数据 Person p1, p2, p3, p4, p5; strcpy_s(p1.name, sizeof("aaa"), "aaa"); strcpy_s(p2.name, sizeof("bbb"), "bbb"); strcpy_s(p3.name, sizeof("ccc"), "ccc"); strcpy_s(p4.name, sizeof("ddd"), "ddd"); strcpy_s(p5.name, sizeof("eee"), "eee"); p1.age = 10; p2.age = 20; p3.age = 30; p4.age = 40; p5.age = 50; //将结点插入链表 Insert_LinkList(list, 0, (LinkNode*)&p1); Insert_LinkList(list, 0, (LinkNode*)&p2); Insert_LinkList(list, 0, (LinkNode*)&p3); Insert_LinkList(list, 0, (LinkNode*)&p4); Insert_LinkList(list, 0, (LinkNode*)&p5); //打印 Print_LinkList(list, MyPrint); //删除结点 RemoveByPos_LinkList(list, 2); //打印 printf("---------------\n"); Print_LinkList(list, MyPrint); printf("---------------\n"); //查找 Person findP; strcpy_s(findP.name, sizeof("bbb"), "bbb"); findP.age = 20; int pos = Find_LinkList(list, (LinkNode*)&findP, MyCompare); printf("位置:%d\n", pos); //释放链表内存 FreeMem_LinkList(list); getchar(); return 0; }
原文:https://www.cnblogs.com/duxie/p/11306083.html