太没用了,暗自捶胸,代码写完 老多错误了,信心都没了,╮(╯▽╰)╭ 目前好歹实现了 , 有兴趣的凑合看下吧
===============================================08.12===========================
后面加了查找,删除,结果比较一直出问题,要找std5 结果std0就说找到了,找了好久好久。。。各种debug
然后发现是cmp函数的两个参数写反了,没有警告,没有错误,比较id都正常,比较名字就出问题
==================主函数=================================================
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"list.h"
#include<string.h>
#include<stdlib.h>
#include"list.h"
#define SIZE 32
typedef struct _score_st
{
int id;
char name[SIZE];
int math;
int chinese;
}score_st;
typedef struct _score_st
{
int id;
char name[SIZE];
int math;
int chinese;
}score_st;
void print_s(void *record)
{
const score_st *r = record;
printf("%d %s %d %d\n",r->id,r->name, r->math,r->chinese);
}
int id_cmp(void *key, void *record)
{
int *k = key;
score_st *r =record;
return (*k -r->id);
}
int name_cmp(void *key,void *record)
{
char *k = key;
score_st *r = record;
int ret;
ret = strcmp(k,r->name);
return ret;
{
const score_st *r = record;
printf("%d %s %d %d\n",r->id,r->name, r->math,r->chinese);
}
int id_cmp(void *key, void *record)
{
int *k = key;
score_st *r =record;
return (*k -r->id);
}
int name_cmp(void *key,void *record)
{
char *k = key;
score_st *r = record;
int ret;
ret = strcmp(k,r->name);
return ret;
}
int main()
int main()
{
LLIST *handle;
int i = 0;
int ret = 0;
score_st tmp;
int i = 0;
int ret = 0;
score_st tmp;
handle = llist_create(sizeof( score_st));
for (i = 0; i<7;i++)
{
tmp.id = i;
snprintf(tmp.name, SIZE,"std%d",i);
tmp.math = rand()%100;
tmp.chinese = rand()%100;
{
tmp.id = i;
snprintf(tmp.name, SIZE,"std%d",i);
tmp.math = rand()%100;
tmp.chinese = rand()%100;
ret = llist_insert(handle,&tmp,LLIST_AFTERWARD);
if(ret)
exit(1);
}
if(ret)
exit(1);
}
llist_travel(handle,print_s);
int id = 3;
printf("===================find===========\n");
char *del_name = "std5";
score_st *data;
data = llist_find(handle, del_name,name_cmp);
if(NULL ==data)
printf("fail to find it\n");
else
print_s(data);
/*
printf("=========delete=======\n");
char *del_name = "kkkk";
ret = llist_delete(handle, del_name, name_cmp);
if(ret ==-1)
printf("can't find it\n");
llist_travel(handle, print_s);
llist_fetch(handle,del_name,name_cmp,data);
print_s(data);
printf("==============fetch============\n");
llist_travel(handle,print_s);*/
llist_destroy(handle);
return 0;
}
=======================================list.c=============================
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"list.h"
#include<string.h>
#include<stdlib.h>
#include"list.h"
LLIST *llist_create(int size)
{
LLIST *new;
new = malloc(sizeof(*new));
if (NULL == new)
return NULL;
{
LLIST *new;
new = malloc(sizeof(*new));
if (NULL == new)
return NULL;
new->size = size;
new->head.data = NULL;
new->head.next = &new->head;
new->head.data = NULL;
new->head.next = &new->head;
new->head.prev = &new->head;
return new;
return new;
}
void llist_destroy(LLIST* ptr)
{
llist_node_st *cur;
llist_node_st *next;
for (cur = ptr->head.next; cur != &ptr->head ;cur = next)
{
next = cur->next;
free(cur->data);
free(cur);
cur = next;
{
llist_node_st *cur;
llist_node_st *next;
for (cur = ptr->head.next; cur != &ptr->head ;cur = next)
{
next = cur->next;
free(cur->data);
free(cur);
cur = next;
}
free(ptr);
}
int llist_insert(LLIST *ptr,const void *data, int mode )
{
llist_node_st *new;
new = malloc(sizeof(*new));
if(new == NULL)
return -1;
new->data = malloc(ptr->size);
if (new->data == NULL)
return -2;
memcpy(new->data,data, ptr->size);
if(mode ==LLIST_FORWARD)
{
new->prev =&ptr->head;
new->next = ptr->head.next;
new->prev->next = new;
new->next->prev = new;
}
else if(mode == LLIST_AFTERWARD)
{
new->next = &ptr->head;
new->prev = ptr->head.prev;
new->prev->next = new;
new->next->prev = new;
}
else
return -3;
return 0;
}
{
llist_node_st *new;
new = malloc(sizeof(*new));
if(new == NULL)
return -1;
new->data = malloc(ptr->size);
if (new->data == NULL)
return -2;
memcpy(new->data,data, ptr->size);
if(mode ==LLIST_FORWARD)
{
new->prev =&ptr->head;
new->next = ptr->head.next;
new->prev->next = new;
new->next->prev = new;
}
else if(mode == LLIST_AFTERWARD)
{
new->next = &ptr->head;
new->prev = ptr->head.prev;
new->prev->next = new;
new->next->prev = new;
}
else
return -3;
return 0;
}
void llist_travel(LLIST *ptr, llist_op *op)
{
{
llist_node_st *save;
for(save = ptr->head.next;save!=&ptr->head;save = save->next)
{
op(save->data);
}
{
op(save->data);
}
}
static llist_node_st *find_(LLIST *ptr, void *key, llist_cmp *cmp)
{
{
llist_node_st *cur;
for (cur = ptr->head.next; cur!= &ptr->head;cur = cur->next)
{
if( cmp(key,cur->data)==0)
{
break;
}
}
return cur;
for (cur = ptr->head.next; cur!= &ptr->head;cur = cur->next)
{
if( cmp(key,cur->data)==0)
{
break;
}
}
return cur;
}
void *llist_find(LLIST *ptr, void *key, llist_cmp *cmp)
{
return find_(ptr, key, cmp)->data;
}
int llist_fetch(LLIST *ptr, void *key, llist_cmp *cmp,void *data)
{
llist_node_st *node;
node = find_(ptr, key, cmp);
if(node==&ptr->head)
return -1;
node->prev->next = node->next;
node->next->prev = node->prev;
return -1;
node->prev->next = node->next;
node->next->prev = node->prev;
if(data != NULL)
memcpy(node->data, data, ptr->size);
free(node->data);
free(node);
return 0;
memcpy(node->data, data, ptr->size);
free(node->data);
free(node);
return 0;
}
int llist_delete( LLIST *ptr, void *key, llist_cmp * cmp)
{
llist_node_st *node;
node = find_(ptr, key, cmp);
if(node == &ptr->head)
return -1;
node->prev->next = node->next;
node->next->prev = node->prev;
free(node->data);
free(node);
{
llist_node_st *node;
node = find_(ptr, key, cmp);
if(node == &ptr->head)
return -1;
node->prev->next = node->next;
node->next->prev = node->prev;
free(node->data);
free(node);
return 0;
}
=================================list.h========================================
#ifndef LIST_H__
#define LIST_H__
#define LIST_H__
#define LLIST_FORWARD 1
#define LLIST_AFTERWARD 2
#define LLIST_AFTERWARD 2
typedef struct _llist_node_st
{
void *data;
struct _llist_node_st *prev;
struct _llist_node_st *next;
}llist_node_st;
typedef struct _LLIST
{
int size;
llist_node_st head;
{
int size;
llist_node_st head;
}LLIST;
typedef void llist_op(void *);
typedef int llist_cmp( void *, void*);
typedef int llist_cmp( void *, void*);
LLIST *llist_create(int size);
void llist_destroy(LLIST* );
int llist_insert(LLIST *,const void *data, int mode );
void *llist_find(LLIST *, void *key, llist_cmp *);
int llist_fetch(LLIST *, void *key, llist_cmp *, void *);
int llist_delete( LLIST *, void *key, llist_cmp *);
//list_fetch();
//list_fetch();
//llist_delete();
void llist_travel(LLIST *,llist_op *op);
#endif
#endif