# 单链表（C实现）

#ifndef _LIST_H        //list.h
#define _LIST_H

typedef struct _node
{
<span style="white-space:pre">	</span>void *data;
<span style="white-space:pre">	</span>struct _node *next;
}NODE;

typedef struct
{
<span style="white-space:pre">	</span>NODE *head;
<span style="white-space:pre">	</span>NODE *last;
<span style="white-space:pre">	</span>int length;
}LIST;
LIST *InitList();
int InsertList(LIST *l,void *data,int size);
NODE * FindNodeByKey(LIST *l,void *key,int (*compare)(void *,void *));
int DeleteListByKey(LIST *l,void *key,int (*compare)(void *,void *));

#endif

//顺序表和链表是如何实现通用性的
//typedef  void *
#include "list.h"		//main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct STU
{
char sno[5];
char name[21];
int age;
float score;
}s[3]={
{"S001","lin wu",12,90},
{"S002","xiao ming",18,80},
{"S003","hu wu",19,70}
};
//也可以通过比较其他的键值来查找，比如年龄，分数。
int CompareByName(void *info,void *key)
{
struct STU *stu=(struct STU *)info;
char *name=(char *)key;
return strcmp(stu->name,name)==0?1:0;
}
int CompareBySno(void *info,void *key)
{
struct STU *stu=(struct STU *)info;
char *sno=(char*)key;
return strcmp(stu->sno,sno)==0?1:0;
}

void main()
{
int i;
NODE *p=NULL;
char name[]="xiao ming",sno[]="S001";
NODE *res=NULL;
LIST *list=InitList();
for(i=0;i<3;i++)
{	InsertList(list,&s[i],sizeof(s[i]));}
res=FindNodeByKey(list,name,CompareByName);
if(res==NULL)
printf("Not Find\n");
else
printf("Find\n");

if(DeleteListByKey(list,sno,CompareBySno))
printf("delete success\n");
else
printf("delete fail\n");

}

VC6运行效果图

VC6调试过程

﻿﻿

• 本文已收录于以下专栏：

举报原因： 您举报文章：单链表（C实现） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)