list.h
#ifndef LIST_H
#define LIST_H
#include <stdbool.h>
typedef int (*Compar)(const void*,const void*);
typedef struct Node
{
void* ptr;
struct Node* prev;
struct Node* next;
}Node;
typedef struct List
{
Node* head;
size_t size;
}List;
List* create_list(void);
void add_list(List* list,void* ptr);
bool insert_list(List* list,size_t index,void* ptr);
bool del_index_list(List* list,size_t index);
bool del_value_list(List* list,void* ptr,Compar cmp);
void* qurey_list(List* list,void* ptr,Compar cmp);
void* visit_list(List* list,size_t index);
void sort_list(List* list,Compar cmp);
void clean_list(List* list);
void destory_list(List* list);
void show_list(List* list,void (*show)(void* ));
#endif
list.c
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "list.h"
static Node* create_node(void* ptr)
{
Node* node = malloc(sizeof(Node));
node->ptr = ptr;
node->prev = node;
node->next = node;
return node;
}
static void _add_list(Node* prev,Node* next,void* ptr)
{
Node* node = create_node(ptr);
prev->next = node;
next->prev = node;
node->prev = prev;
node->next = next;
}
static void _del_list(Node* node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
free(node->ptr);
free(node);
}
static Node* _visit_list(List* list,size_t index)
{
if(index >= list->size) return NULL;
if(index < list->size/2)
{
Node* node = list->head->next;
while(index--) node = node->next;
return node;
}
else
{
Node* node = list->head->prev;
while(++index < list->size) node = node->prev;
return node;
}
}
List* create_list(void)
{
List* list = malloc(sizeof(List));
list->head = create_node(0);
list->size = 0;
return list;
}
void add_list(List* list,void* ptr)
{
_add_list(list->head->prev,list->head,ptr);
list->size++;
}
bool insert_list(List* list,size_t index,void* ptr)
{
Node* node = _visit_list(list,index);
if(NULL == node) return false;
_add_list(node->prev,node,ptr);
list->size++;
return true;
}
bool del_index_list(List* list,size_t index)
{
Node* node = _visit_list(list,index);
if(NULL == node) return false;
_del_list(node);
list->size--;
return true;
}
bool del_value_list(List* list,void* ptr,Compar cmp)
{
for(Node* n=list->head->next; n!=list->head; n=n->next)
{
if(0 == cmp(ptr,n->ptr))
{
_del_list(n);
list->size--;
return true;
}
}
return false;
}
void* qurey_list(List* list,void* ptr,Compar cmp)
{
for(Node* n=list->head->next; n!=list->head; n=n->next)
{
if(0 == cmp(ptr,n->ptr))
return n->ptr;
}
return NULL;
}
void* visit_list(List* list,size_t index)
{
Node* node = _visit_list(list,index);
if(NULL == node) return NULL;
return node->ptr;
}
void sort_list(List* list,Compar cmp)
{
for(Node* n=list->head->next; n->next!=list->head; n=n->next)
{
for(Node* m=n->next; m!=list->head; m=m->next)
{
if(-1 == cmp(n->ptr,m->ptr))
{
void* tmp = n->ptr;
n->ptr = m->ptr;
m->ptr = tmp;
}
}
}
}
void clean_list(List* list)
{
Node* node = list->head->next;
while(list->size--)
{
Node* temp = node;
node = node->next;
free(temp->ptr);
free(temp);
}
list->head->next = list->head;
list->head->prev = list->head;
}
void destory_list(List* list)
{
clean_list(list);
free(list->head->ptr);
free(list->head);
free(list);
}
void show_list(List* list,void (*show)(void* ))
{
for(Node* n=list->head->next; n!=list->head; n=n->next)
{
show(n->ptr);
}
}
测试test.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list.h"
typedef struct Student
{
char name[20];
char sex;
int id;
}Student;
void show_stu(void* ptr)
{
Student* stu = ptr;
printf("姓名:%s 性别:%c 学号:%d\n",stu->name,stu->sex,stu->id);
}
int stucmp(const void* s1,const void* s2)
{
const Student* stu1 = s1;
const Student* stu2 = s2;
if(stu1->id > stu2->id)
return -1;
if(stu1->id < stu2->id)
return 1;
return 0;
}
int main(int argc,const char* argv[])
{
List* list = create_list();
for(int i=0; i<10; i++)
{
Student* stu = malloc(sizeof(Student));
strcpy(stu->name,"hehe");
stu->sex = i%2?'w':'m';
stu->id = 1001+i;
add_list(list,stu);
}
show_list(list,show_stu);
printf("-------------\n");
Student stu = {"xixi",'w',1011};
clean_list(list);
for(int i=0; i<10; i++)
{
Student* stu = malloc(sizeof(Student));
strcpy(stu->name,"hehe");
stu->sex = i%2?'w':'m';
stu->id = 1001+i;
add_list(list,stu);
}
show_list(list,show_stu);
destory_list(list);
for(int i=0; i<10; i++)
{
Student* stu = malloc(sizeof(Student));
strcpy(stu->name,"hehe");
stu->sex = i%2?'w':'m';
stu->id = 1001+i;
add_list(list,stu);
}
show_list(list,show_stu);
}