↩ revert:
This commit is contained in:
@@ -7,8 +7,8 @@
|
||||
// !!! 变长结构体
|
||||
struct llist_node_st
|
||||
{
|
||||
struct llist_node_st* prev;
|
||||
struct llist_node_st* next;
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
// data要放在最后
|
||||
char data[1]; // 占位符,数据的起始。C99才只是[0]
|
||||
};
|
||||
@@ -19,9 +19,9 @@ struct llist_head_st
|
||||
struct llist_node_st head;
|
||||
};
|
||||
|
||||
LLIST* llist_create(int initsize)
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
struct llist_head_st* new;
|
||||
struct llist_head_st *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
@@ -34,10 +34,10 @@ LLIST* llist_create(int initsize)
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST* p, const void* data, int mode)
|
||||
int llist_insert(LLIST *p, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st* newnode;
|
||||
struct llist_head_st* ptr = p;
|
||||
struct llist_node_st *newnode;
|
||||
struct llist_head_st *ptr = p;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
@@ -67,11 +67,11 @@ int llist_insert(LLIST* p, const void* data, int mode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st* find_(struct llist_head_st* ptr,
|
||||
const void* key,
|
||||
llist_cmp* cmp)
|
||||
static struct llist_node_st *find_(struct llist_head_st *ptr,
|
||||
const void *key,
|
||||
llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st* cur;
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
{
|
||||
@@ -82,10 +82,10 @@ static struct llist_node_st* find_(struct llist_head_st* ptr,
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void* llist_find(LLIST* p, const void* key, llist_cmp* cmp)
|
||||
void *llist_find(LLIST *p, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_head_st* ptr = p;
|
||||
struct llist_node_st* node;
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
@@ -93,10 +93,10 @@ void* llist_find(LLIST* p, const void* key, llist_cmp* cmp)
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST* p, const void* key, llist_cmp* cmp)
|
||||
int llist_delete(LLIST *p, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_head_st* ptr = p;
|
||||
struct llist_node_st* node;
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -109,10 +109,10 @@ int llist_delete(LLIST* p, const void* key, llist_cmp* cmp)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST* p, const void* key, llist_cmp* cmp, void* data)
|
||||
int llist_fetch(LLIST *p, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_head_st* ptr = p;
|
||||
struct llist_node_st* node;
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -129,18 +129,18 @@ int llist_fetch(LLIST* p, const void* key, llist_cmp* cmp, void* data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST* p, llist_op* op)
|
||||
void llist_travel(LLIST *p, llist_op *op)
|
||||
{
|
||||
struct llist_head_st* ptr = p;
|
||||
struct llist_node_st* cur;
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST* p)
|
||||
void llist_destroy(LLIST *p)
|
||||
{
|
||||
struct llist_head_st* ptr = p;
|
||||
struct llist_head_st *ptr = p;
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = next)
|
||||
|
||||
@@ -7,21 +7,21 @@
|
||||
// !!! 实现了隐藏 LLIST 的实现方法
|
||||
typedef void LLIST;
|
||||
|
||||
typedef void llist_op(const void*);
|
||||
typedef int llist_cmp(const void*, const void*);
|
||||
typedef void llist_op(const void *);
|
||||
typedef int llist_cmp(const void *, const void *);
|
||||
|
||||
LLIST* llist_create(int initsize);
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
int llist_insert(LLIST*, const void* data, int mode);
|
||||
int llist_insert(LLIST *, const void *data, int mode);
|
||||
|
||||
void* llist_find(LLIST*, const void* key, llist_cmp*);
|
||||
void *llist_find(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_delete(LLIST*, const void* key, llist_cmp*);
|
||||
int llist_delete(LLIST *, const void *key, llist_cmp *);
|
||||
|
||||
int llist_fetch(LLIST*, const void* key, llist_cmp*, void* data);
|
||||
int llist_fetch(LLIST *, const void *key, llist_cmp *, void *data);
|
||||
|
||||
void llist_travel(LLIST*, llist_op*);
|
||||
void llist_travel(LLIST *, llist_op *);
|
||||
|
||||
void llist_destroy(LLIST*);
|
||||
void llist_destroy(LLIST *);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -14,25 +14,25 @@ struct score_st
|
||||
int chinese;
|
||||
};
|
||||
|
||||
void print_s(const void* record)
|
||||
void print_s(const void *record)
|
||||
{
|
||||
const struct score_st* r = record;
|
||||
const struct score_st *r = record;
|
||||
|
||||
printf("%d %s %d %d\n", r->id, r->name, r->math, r->chinese);
|
||||
}
|
||||
|
||||
int id_cmp(const void* key, const void* record)
|
||||
int id_cmp(const void *key, const void *record)
|
||||
{
|
||||
const int* k = key;
|
||||
const struct score_st* r = record;
|
||||
const int *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return (*k - r->id);
|
||||
}
|
||||
|
||||
int name_cmp(const void* key, const void* record)
|
||||
int name_cmp(const void *key, const void *record)
|
||||
{
|
||||
const char* k = key;
|
||||
const struct score_st* r = record;
|
||||
const char *k = key;
|
||||
const struct score_st *r = record;
|
||||
|
||||
return strcmp(k, r->name);
|
||||
}
|
||||
@@ -40,7 +40,7 @@ int name_cmp(const void* key, const void* record)
|
||||
|
||||
int main( )
|
||||
{
|
||||
LLIST* handler;
|
||||
LLIST *handler;
|
||||
int i, ret;
|
||||
struct score_st tmp;
|
||||
|
||||
@@ -65,7 +65,7 @@ int main( )
|
||||
printf("\n\n");
|
||||
|
||||
// int id = 3;
|
||||
char* del_name = "std6";
|
||||
char *del_name = "std6";
|
||||
|
||||
// ret = llist_delete(handler, &id, id_cmp);
|
||||
ret = llist_delete(handler, del_name, name_cmp);
|
||||
|
||||
Reference in New Issue
Block a user