企业链表-Linux内核链表优化简单实现
头文件:LinuxKernelList.h
#ifndef LINUXKERNELLIST_H
#define LINUXKERNELLIST_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct LINUXNODE {
struct LINUXNODE* next;
}LinuxNode;
typedef struct LINUXLIST {
LinuxNode head;
int size;
}LinuxList;
typedef void(*PRINTNODE)(LinuxNode*);
typedef int(*COMPARENODE)(LinuxNode*, LinuxNode*);
LinuxList* INIT_LINUXLIST();
void Insert_LinuxList(LinuxList* list, int pos, LinuxNode* data);
void Remove_LinuxList(LinuxList* list, int pos);
int Find_LinuxList(LinuxList* list, LinuxNode* data ,COMPARENODE compare);
int Size_LinuxList(LinuxList* list);
void Print_LinuxList(LinuxList* list, PRINTNODE print);
void FreeSpace_LinuxList(LinuxList* list);
#endif
实现文件:LinuxKernelList.c
#include "LinuxKernelList.h"
LinuxList* INIT_LINUXLIST() {
LinuxList* list = (LinuxList*)malloc(sizeof(LinuxList));
list->head.next = NULL;
list->size = 0;
return list;
}
void Insert_LinuxList(LinuxList* list, int pos, LinuxNode* data) {
if (list == NULL) {
return;
}
if (data == NULL) {
return;
}
if (pos<0 || pos>list->size) {
pos = list->size;
}
LinuxNode* pCurrent = &(list->head);
for (int i = 0; i < pos; i++) {
pCurrent = pCurrent->next;
}
data->next = pCurrent->next;
pCurrent->next = data;
list->size++;
}
void Remove_LinuxList(LinuxList* list, int pos) {
if (list == NULL) {
return;
}
if (pos<0 || pos>list->size) {
pos = list->size;
}
LinuxNode* pCurrent = &(list->head);
for (int i = 0; i < pos; i++) {
pCurrent = pCurrent->next;
}
pCurrent->next = pCurrent->next->next;
list->size--;
}
int Find_LinuxList(LinuxList* list, LinuxNode* data, COMPARENODE compare) {
if (list == NULL) {
return-1;
}
if (data == NULL) {
return-1;
}
LinuxNode* 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_LinuxList(LinuxList* list) {
return list->size;
}
void Print_LinuxList(LinuxList* list, PRINTNODE print) {
if (list == NULL) {
return;
}
LinuxNode* pCurrent = list->head.next;
while (pCurrent != NULL) {
print(pCurrent);
pCurrent = pCurrent->next;
}
}
void FreeSpace_LinuxList(LinuxList* list) {
if (list == NULL) {
return;
}
free(list);
}
主文件:Main.c
typedef struct PERSON_2{
LinuxNode node;
char name[64];
int age;
}Person_2;
void MyPrint2(LinuxNode*data) {
Person_2* p = (Person_2*)data;
printf("姓名:%-10s 年龄:%-10d\n", p->name, p->age);
}
int MyCompare(LinuxNode* pCurrent,LinuxNode*data) {
Person_2* p1 = (Person_2*)pCurrent;
Person_2* p2 = (Person_2*)data;
if (strcmp(p1->name , p2->name)==0 && p1->age==p2->age) {
return 0;
}
return -1;
}
void LinuxListTest() {
LinkList* list = INIT_LINUXLIST();
Person_2 p1, p2, p3, p4, p5;
strcpy(p1.name, "Tom");
strcpy(p2.name, "Bob");
strcpy(p3.name, "Lili");
strcpy(p4.name, "Bryan");
strcpy(p5.name, "Hugo");
p1.age = 45;
p2.age = 16;
p3.age = 65;
p4.age = 19;
p5.age = 26;
Insert_LinuxList(list, 0, (LinuxNode*)&p1);
Insert_LinuxList(list, 0, (LinuxNode*)&p2);
Insert_LinuxList(list, 0, (LinuxNode*)&p3);
Insert_LinuxList(list, 0, (LinuxNode*)&p4);
Insert_LinuxList(list, 0, (LinuxNode*)&p5);
Print_LinuxList(list, MyPrint2);
printf("------------------------\n");
Remove_LinuxList(list, 2);
Print_LinuxList(list, MyPrint2);
Person_2 p6;
strcpy(p6.name, "Bob");
p6.age = 16;
int index = Find_LinuxList(list, (LinuxNode*)&p6, MyCompare);
printf("------------------------\n");
printf("链表中存在这个人,他的索引是:%d\n", index);
FreeSpace_LinuxList(list);
}
int main(){
LinuxListTest();
system("pause");
return 0;
}