头文件
`
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
//定义基础的结构
typedef struct ListElmt_{
void *data;
struct ListElmt_ *next;
}ListElmt;
//定义列表
typedef struct list_{
int size;//大小
int (*match)(const void *key1,const void *key2);//比较,匹配
void (*destroy)(void *data);//内存释放函数
ListElmt *head;//头
ListElmt *tail;//尾部
}List;
//初始化
void list_init(List *list,void(*destroy)(void *data));
//在element元素后插入一个元素,如果element为空,则插入链表头部
int list_ins_next(List *list, ListElmt *element,const void *data);
//删除element后面的那个元素,如果element为空,则删除链表头元素
int list_rem_next(List *list,ListElmt *element,void **data);
//删除一个列表,删除列表里面存储的data
void list_destroy(List *list);
//定义基础的宏,获取各种基础的数据:获取大小,头,尾部,判断是头,是尾,数据,下一个元素
#define list_size(list) ((list)->size)
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_is_head(list,element) ((element) == (list)->head?1:0)
#define list_is_tail(element) ((element)->next == NULL?1:0)
#define list_data(element)((element)->data)
#define list_next(element)((element)->next)
#endif
`
实现代码
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "list.h"
void list_init(List *list,void(*destroy)(void *data)){
list->size = 0;
list->destroy = destroy;
list->head = list->tail = NULL;
return;
}
//在element元素后插入一个元素,
//如果element为空,则插入链表头部
//需要考虑链表为空的情况
int list_ins_next(List *list, ListElmt *element,const void *data){
//创建新数据
ListElmt *new_element;
if((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
return -1;
new_element->data = (void *)data;
new_element->next = NULL;
//如果选定数据为空,插入到表头
if(element == NULL){
//如果列表为空,尾部也指向新元素
if(list_size(list) == 0)
list->tail = new_element;
//在头链表前插入
new_element->next = list->head;
list->head = new_element;
}else{
//如果是最后一个元素
if(element->next == NULL)
list->tail = new_element;
//在元素后插入
new_element->next = element->next;
element->next = new_element;
}
list->size ++;
return 0;
}
//删除element后面的那个元素
//如果element为空,则删除链表头元素
int list_rem_next(List *list,ListElmt *element,void **data){
//队列不为空
if(list_size(list) == 0){
return -1;
}
ListElmt *old_element;
if(element == NULL){
//头部删除一个元素
*data = list->head->data;
old_element = list->head;
list->head = list->head->next;
if(list_size(list) == 1) {
list->tail = NULL;
}
}else{
//尾部删除一个元素
if(element->next == NULL){
return -1;
}
*data = element->next->data;
old_element = element->next;
element->next = element->next->next;
if(element->next == NULL){
list->tail = element;
}
}
free(old_element);
list->size --;
return 0;
}
//删除一个列表,删除列表里面存储的data
void list_destroy(List *list){
void *data;
while(list_size(list) > 0){
if(list_rem_next(list,NULL,(void **) &data) == 0 && list->destroy != NULL){
list->destroy(data);
}
}
memset(list,0,sizeof(List));
return;
}
测试代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ONE_MB (1024 * 1024)
#include "list.h"
typedef struct Info_{
int id;
long phone;
}Info;
//打印一个列表
void print_list_int(List *list);
void print_list_info(List *list);
void show_memory();
void testFree();
void tlist();
void tlistArr();
void tlistArrPoint();
int main(int argc, char const *argv[])
{
//tlistArr();
//tlistArrPoint();
tlist();
return 0;
}
void tlistArrPoint(){
List *listArr[3];
List *plist;
listArr[0] = (List *)malloc(sizeof(List));
plist = listArr[0];
list_init(plist,free);
Info *pi2 = (Info*)malloc(sizeof(Info));
pi2->id = 123131;
pi2->phone = 13012131231;
list_ins_next(plist,NULL,(void*)pi2);
printf("plist->size:%d\n", plist->size);
print_list_info(plist);
}
//测试list array
void tlistArr(){
List listArr[3];
List *plist;
plist = &listArr[0];
list_init(plist,free);
Info *pi2 = (Info*)malloc(sizeof(Info));
pi2->id = 123131;
pi2->phone = 13012131231;
list_ins_next(plist,NULL,(void*)pi2);
printf("plist->size:%d\n", plist->size);
print_list_info(plist);
}
void tlist(){
printf("test list\n");
List *plist;
plist = (List *)malloc(sizeof(List));
list_init(plist,free);
printf("plist->size:%d\n", plist->size );
Info *pi2 = (Info*)malloc(sizeof(Info));
pi2->id = 123131;
pi2->phone = 13012131231;
list_ins_next(plist,NULL,(void*)pi2);
Info *pi3 = (Info*)malloc(sizeof(Info));
pi3->id = 5370569;
pi3->phone = 18611080217;
list_ins_next(plist,NULL,(void*)pi3);
Info *pi4 = (Info*)malloc(sizeof(Info));
pi4->id = 2231313;
pi4->phone = 18643521217;
list_ins_next(plist,NULL,(void*)pi4);
//print list
printf("plist->size:%d\n", plist->size);
print_list_info(plist);
//delete
void **ppi = (void**)malloc(sizeof(Info));
list_rem_next(plist,NULL,ppi);
//print list
printf("plist->size:%d\n", plist->size );
print_list_info(plist);
//show_memory();
//destroy
list_destroy(plist);
//show_memory();
//testFree();
}
//打印一个列表
void print_list_info(List *list){
if(list_size(list) == 0){
printf("empty list\n");
return;
}
ListElmt *pl;
pl = list_head(list);
int i;
printf("#########start#############\n");
for(i=0;i<list_size(list);i++){
printf("id=%d,phone=%ld\n",((Info*)list_data(pl))->id,((Info*)list_data(pl))->phone);
pl = pl->next;
}
printf("#########end#############\n");
}
void testFree(){
struct stu
{
int num;
char *name;
char sex;
float score;
} *ps;
ps=(struct stu*)malloc(sizeof(struct stu));
ps->num=102;
ps->name="Zhang ping";
ps->sex='M';
ps->score=62.5;
printf("Number=%d Name=%s ",ps->num,ps->name);
printf("sex=%c Score=%f ",ps->sex,ps->score);
free(ps);
}
//打印一个列表
void print_list_int(List *list){
if(list_size(list) == 0){
printf("empty list\n");
return;
}
ListElmt *pl;
pl = list_head(list);
int i;
for(i=0;i<list_size(list);i++){
printf("%d=>%d,",i+1,*(int*)pl->data);
pl = pl->next;
}
printf("\n");
}