新建一个的头文件stu.h

#ifndef _STU_H
#define _STU_H
typedef struct _stu
{
char sno[5]; //年纪
char name[21]; //姓名
int age; //年龄
int score; //得分
}stu;
#endif

新建一个的头文件.h

#ifndef _LIST_H
#define _LIST_H
typedef struct _node
{
void* data; //数据域
struct _node *next; //指针域
}Node; //节点

typedef struct _LIST
{
Node* head;
Node* last;
int length;
}LIST; //链表

LIST* InitList();
int InsertList(LIST* List, void *data, int size);
Node* FindNodeByName(LIST* List,void* Key,int (*compare)(void* ,void*));
int DeleteNodeByName(LIST* List,void* Key,int (*compare)(void* ,void*));
#endif

新建list.c

#include "stu.h"
#include "list.h"
#include <stdlib.h>
#include <windows.h>
#include <string.h>

LIST *InitList(){
LIST* List = (LIST*)malloc(sizeof(LIST));
if (List == NULL){
exit(0);
}
memset(List, 0, sizeof(LIST));
return List;
}

int InsertList(LIST* List, void *data, int size)
{
Node* n;
if (List == NULL || data == NULL){
return 0;
}
n = (Node*)malloc(sizeof(Node));
if (n == NULL){
return 0;
}
n->data = malloc(size);
if (n->data == NULL){
free(n);
return 0;
}
memcpy(n->data, data, size);
n->next = NULL;
if (List->head == NULL){
List->head = n;
List->last = n;
List->length = 1;
}
else{
List->last->next = n;
List->last = n;
List->length++;
}
return 1;
}

Node* FindNodeByName(LIST* List, void* key, int(*compare)(void*, void*)){
Node* p = NULL;
if (List == NULL || key == NULL || compare == NULL ){
return 0;
}
p = List->head;
while (p){
if (compare(p->data, key) == 1){
return p;
}
p = p->next;
}
return NULL;
}



Node* FindPre(LIST* List, void* key, int(*compare)(void*, void*)){
Node* p = NULL,*q=NULL;
if (List == NULL || key == NULL || compare == NULL){
return 0;
}
Node* pre = NULL;
p = List->head;

while (p){
if (compare(p->data, key) == 1){
pre = q;
}
q = p;
p = p->next;
}
return pre;
}


int DeleteNodeByName(LIST* List, void* key, int(*compare)(void*, void*)){
Node* p = NULL, *q = NULL;

p = List->head;

//第一个就找到了
if (compare(p->data, key) == 1){
List->head = p->next;
}
//第二个找到了
else if (compare(p->next->data, key) == 1){
List->head->next = p->next->next;
}
else{
p = FindPre(List, key, compare);
if (p == NULL){
return 0;
}


p->next = p->next->next;
}

List->length--;


}

新建入口文件main.c

#include "stu.h"
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
stu ss[4] = {
{ "S01", "张三", 22, 100 },
{ "S02", "王五", 23, 90 },
{ "S03", "小红", 24, 80 },
{ "S03", "小花", 24, 80 }
};

int CompareNodeByName(void* info, void* key){
stu *st = (stu*)info;
char* name = (char*)key;
return strcmp(st->name, name) == 0?1:0;
}

int main()
{
LIST *List = InitList();
Node* node;
int i;

for (i = 0; i<4; i++)
{
InsertList(List, &ss[i], sizeof(ss[i]));
}


node = List->head;


while (node!=NULL){
printf("%s\t%s\t\n", ((stu*)(node->data))->sno, ((stu*)(node->data))->name);
node = node->next;
}

char* key = "小花";
if (FindNodeByName(List,key , CompareNodeByName)){
printf("find\n");
}
else{
printf("Not find\n");
}

DeleteNodeByName(List, key, CompareNodeByName);
node = List->head;
while (node != NULL){
printf("%s\t%s\t\n", ((stu*)(node->data))->sno, ((stu*)(node->data))->name);
node = node->next;
}
system("pause");
return 0;
}

输出结果:

S01     张三
S02 王五
S03 小红
S03 小花
find
S01 张三
S02 王五
S03 小红