标签:-- 结构 eee html include title log src har
https://www.cnblogs.com/wanghui1234/p/11196467.html
LinkList.h
#ifndef LINKLIST_H #define LINKLIST_H #include<stdlib.h> #include<stdio.h> //链表小结点 typedef struct LINKNODE { //相当于挂钩 struct LINKNODE* next; }LinkNode; //链表结点 typedef struct LINKLIST { LinkNode head; int size; }LinkList; //遍历函数指针 typedef void(*PRINTNODE)(LinkNode*); //比较函数指针 typedef int(*COMPARENODE)(LinkNode*, LinkNode*); //初始化链表 LinkList* Init_LinkList(); //插入 void Insert_LinkList(LinkList* list, int pos, LinkNode* data); //删除 void Remove_LinkList(LinkList* list, int pos); //查找 int Find_LinkList(LinkList* list, LinkNode* data, COMPARENODE compare); //返回链表大小 int Size_LinkList(LinkList* list); //打印 void Print_LinkList(LinkList* list, PRINTNODE print); //释放链表内存 void FreeSpace_LinkList(LinkList* list); #endif
LinkList.c
#include"LinkList.h" //初始化链表 LinkList* Init_LinkList() { LinkList* list = (LinkList*)malloc(sizeof(LinkList)); list->head.next = NULL; list->size = 0; return list; } //插入 void Insert_LinkList(LinkList* list, int pos, LinkNode* data) { if (list == NULL) { return; } if (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 Remove_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) { return -1; } if (data == NULL) { return -1; } //赋值指针变量 LinkNode* pCurrent = list->head.next; int index = 0; int flag = -1; while (pCurrent != NULL) { if (compare(pCurrent, data) == 0) { flag = index; break; } pCurrent = pCurrent->next; index++; } return flag; } //返回链表大小 int Size_LinkList(LinkList* list) { return 0; } //打印 void Print_LinkList(LinkList* list, PRINTNODE print) { if (list == NULL) { return; } //辅助指针 LinkNode* pCurrent = list->head.next; while (pCurrent != NULL) { print(pCurrent); pCurrent = pCurrent->next; } } //释放链表内存 void FreeSpace_LinkList(LinkList* list) { if (list == NULL) { return; } free(list); }
企业链表.c
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #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 (strcmp(p1->name, p2->name) == 0 && p1->age == p2->age) { return 0; } return -1; } int main(void) { //创建链表 LinkList* list = Init_LinkList(); //创建数据 Person p1, p2, p3, p4, p5; strcpy(p1.name, "aaa"); strcpy(p2.name, "bbb"); strcpy(p3.name, "ccc"); strcpy(p4.name, "ddd"); strcpy(p5.name, "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); //删除结点 Remove_LinkList(list, 2); //打印 printf("---------------\n"); Print_LinkList(list, MyPrint); //查找 Person findP; strcpy(findP.name, "bbb"); findP.age = 20; int pos = Find_LinkList(list, (LinkNode*)&findP, MyCompare); printf("位置:%d\n", pos); //释放链表内存 FreeSpace_LinkList(list); system("pause"); return 0; }
标签:-- 结构 eee html include title log src har
原文地址:https://www.cnblogs.com/wanghui1234/p/11208051.html