↩ revert:
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
|
||||
struct list_head
|
||||
{
|
||||
struct list_head* prev;
|
||||
struct list_head* next;
|
||||
struct list_head *prev;
|
||||
struct list_head *next;
|
||||
};
|
||||
|
||||
#define LIST_HEAD_INIT(name) \
|
||||
@@ -14,9 +14,9 @@ struct list_head
|
||||
|
||||
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
|
||||
|
||||
static inline void __list_add(struct list_head* new,
|
||||
struct list_head* prev,
|
||||
struct list_head* next)
|
||||
static inline void __list_add(struct list_head *new,
|
||||
struct list_head *prev,
|
||||
struct list_head *next)
|
||||
{
|
||||
next->prev = new;
|
||||
new->next = next;
|
||||
@@ -24,7 +24,7 @@ static inline void __list_add(struct list_head* new,
|
||||
prev->next = new;
|
||||
}
|
||||
|
||||
static inline void list_add(struct list_head* new, struct list_head* head)
|
||||
static inline void list_add(struct list_head *new, struct list_head *head)
|
||||
{
|
||||
__list_add(new, head, head->next);
|
||||
}
|
||||
@@ -43,12 +43,12 @@ static inline void list_add(struct list_head* new, struct list_head* head)
|
||||
*
|
||||
*/
|
||||
|
||||
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE*)0)->MEMBER)
|
||||
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)
|
||||
|
||||
#define container_of(ptr, type, member) \
|
||||
({ \
|
||||
const typeof(((type*)0)->member)* __mptr = (ptr); \
|
||||
(type*)((char*)__mptr - offsetof(type, member)); \
|
||||
#define container_of(ptr, type, member) \
|
||||
({ \
|
||||
const typeof(((type *)0)->member) *__mptr = (ptr); \
|
||||
(type *)((char *)__mptr - offsetof(type, member)); \
|
||||
})
|
||||
|
||||
#define list_entry(ptr, type, member) container_of(ptr, type, member)
|
||||
|
||||
@@ -14,7 +14,7 @@ struct score_st
|
||||
int chinese;
|
||||
};
|
||||
|
||||
static void print_s(struct score_st* d)
|
||||
static void print_s(struct score_st *d)
|
||||
{
|
||||
printf("%d %s %d %d \n", d->id, d->name, d->math, d->chinese);
|
||||
}
|
||||
@@ -22,8 +22,8 @@ static void print_s(struct score_st* d)
|
||||
int main( )
|
||||
{
|
||||
int i;
|
||||
struct score_st* datap;
|
||||
struct list_head* cur;
|
||||
struct score_st *datap;
|
||||
struct list_head *cur;
|
||||
|
||||
LIST_HEAD(head);
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
LLIST* llist_create(int initsize)
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST* new;
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
@@ -20,9 +20,9 @@ LLIST* llist_create(int initsize)
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST* ptr, const void* data, int mode)
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st* newnode;
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
@@ -56,9 +56,9 @@ int llist_insert(LLIST* ptr, const void* data, int mode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st* find_(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
static struct llist_node_st *find_(LLIST *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)
|
||||
{
|
||||
@@ -69,14 +69,14 @@ static struct llist_node_st* find_(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void* llist_find(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
return find_(ptr, key, cmp)->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -91,9 +91,9 @@ int llist_delete(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST* ptr, const void* key, llist_cmp* cmp, void* data)
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -111,15 +111,15 @@ int llist_fetch(LLIST* ptr, const void* key, llist_cmp* cmp, void* data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST* ptr, llist_op* op)
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st* cur;
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST* ptr)
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
struct llist_node_st
|
||||
{
|
||||
void* data;
|
||||
struct llist_node_st* prev;
|
||||
struct llist_node_st* next;
|
||||
void *data;
|
||||
struct llist_node_st *prev;
|
||||
struct llist_node_st *next;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
@@ -17,21 +17,21 @@ typedef struct
|
||||
struct llist_node_st head;
|
||||
} 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* ptr, const void* data, int mode);
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
|
||||
void* llist_find(LLIST* ptr, const void* key, llist_cmp*);
|
||||
void *llist_find(LLIST *ptr, 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* ptr, llist_op* op);
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
void llist_destroy(LLIST* ptr);
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#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);
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
LLIST* llist_create(int initsize)
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST* new;
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
@@ -19,9 +19,9 @@ LLIST* llist_create(int initsize)
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST* ptr, const void* data, int mode)
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st* newnode;
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
@@ -51,9 +51,9 @@ int llist_insert(LLIST* ptr, const void* data, int mode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st* find_(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
static struct llist_node_st *find_(LLIST *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)
|
||||
{
|
||||
@@ -64,9 +64,9 @@ static struct llist_node_st* find_(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void* llist_find(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
@@ -74,9 +74,9 @@ void* llist_find(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -89,9 +89,9 @@ int llist_delete(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST* ptr, const void* key, llist_cmp* cmp, void* data)
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -108,15 +108,15 @@ int llist_fetch(LLIST* ptr, const void* key, llist_cmp* cmp, void* data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST* ptr, llist_op* op)
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st* cur;
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST* ptr)
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
|
||||
@@ -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,21 +19,21 @@ typedef struct
|
||||
struct llist_node_st head;
|
||||
} 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* ptr, const void* data, int mode);
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
|
||||
void* llist_find(LLIST* ptr, const void* key, llist_cmp*);
|
||||
void *llist_find(LLIST *ptr, 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* ptr, llist_op* op);
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
void llist_destroy(LLIST* ptr);
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#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);
|
||||
|
||||
@@ -4,15 +4,15 @@
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
int llist_insert(LLIST* ptr, const void* data, int mode);
|
||||
void* llist_find(LLIST* ptr, 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);
|
||||
void llist_travel(LLIST* ptr, llist_op* op);
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode);
|
||||
void *llist_find(LLIST *ptr, 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);
|
||||
void llist_travel(LLIST *ptr, llist_op *op);
|
||||
|
||||
LLIST* llist_create(int initsize)
|
||||
LLIST *llist_create(int initsize)
|
||||
{
|
||||
LLIST* new;
|
||||
LLIST *new;
|
||||
|
||||
new = malloc(sizeof(*new)); // !!! 不是sizeof(initsize),也不是new,是*new
|
||||
if (NULL == new)
|
||||
@@ -31,9 +31,9 @@ LLIST* llist_create(int initsize)
|
||||
return new;
|
||||
}
|
||||
|
||||
int llist_insert(LLIST* ptr, const void* data, int mode)
|
||||
int llist_insert(LLIST *ptr, const void *data, int mode)
|
||||
{
|
||||
struct llist_node_st* newnode;
|
||||
struct llist_node_st *newnode;
|
||||
|
||||
newnode = malloc(sizeof(*newnode) + ptr->size); // -4
|
||||
if (NULL == newnode)
|
||||
@@ -63,9 +63,9 @@ int llist_insert(LLIST* ptr, const void* data, int mode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct llist_node_st* find_(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
static struct llist_node_st *find_(LLIST *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)
|
||||
{
|
||||
@@ -76,9 +76,9 @@ static struct llist_node_st* find_(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return cur; // 找不到的时候,返回的cur就是ptr,返回NULL
|
||||
}
|
||||
|
||||
void* llist_find(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
void *llist_find(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
return NULL;
|
||||
@@ -86,9 +86,9 @@ void* llist_find(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return node->data;
|
||||
}
|
||||
|
||||
int llist_delete(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
int llist_delete(LLIST *ptr, const void *key, llist_cmp *cmp)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -101,9 +101,9 @@ int llist_delete(LLIST* ptr, const void* key, llist_cmp* cmp)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int llist_fetch(LLIST* ptr, const void* key, llist_cmp* cmp, void* data)
|
||||
int llist_fetch(LLIST *ptr, const void *key, llist_cmp *cmp, void *data)
|
||||
{
|
||||
struct llist_node_st* node;
|
||||
struct llist_node_st *node;
|
||||
|
||||
node = find_(ptr, key, cmp);
|
||||
if (node == &ptr->head)
|
||||
@@ -120,15 +120,15 @@ int llist_fetch(LLIST* ptr, const void* key, llist_cmp* cmp, void* data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void llist_travel(LLIST* ptr, llist_op* op)
|
||||
void llist_travel(LLIST *ptr, llist_op *op)
|
||||
{
|
||||
struct llist_node_st* cur;
|
||||
struct llist_node_st *cur;
|
||||
|
||||
for (cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
|
||||
op(cur->data);
|
||||
}
|
||||
|
||||
void llist_destroy(LLIST* ptr)
|
||||
void llist_destroy(LLIST *ptr)
|
||||
{
|
||||
struct llist_node_st *cur, *next;
|
||||
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
#define LLIST_FORWARD 1
|
||||
#define LLIST_BACKWARD 2
|
||||
|
||||
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 *);
|
||||
|
||||
// !!! 变长结构体
|
||||
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]
|
||||
};
|
||||
@@ -21,18 +21,18 @@ typedef struct llist_head
|
||||
int size;
|
||||
struct llist_node_st head;
|
||||
|
||||
int (*insert)(struct llist_head*, const void*, int);
|
||||
void* (*find)(struct llist_head*, const void*, llist_cmp*);
|
||||
int (*delete)(struct llist_head*, const void*, llist_cmp*);
|
||||
int (*fetch)(struct llist_head*, const void*, llist_cmp*, void*);
|
||||
void (*travel)(struct llist_head*, llist_op*);
|
||||
int (*insert)(struct llist_head *, const void *, int);
|
||||
void *(*find)(struct llist_head *, const void *, llist_cmp *);
|
||||
int (*delete)(struct llist_head *, const void *, llist_cmp *);
|
||||
int (*fetch)(struct llist_head *, const void *, llist_cmp *, void *);
|
||||
void (*travel)(struct llist_head *, llist_op *);
|
||||
|
||||
} LLIST;
|
||||
|
||||
|
||||
|
||||
LLIST* llist_create(int initsize);
|
||||
LLIST *llist_create(int initsize);
|
||||
|
||||
void llist_destroy(LLIST* ptr);
|
||||
void llist_destroy(LLIST *ptr);
|
||||
|
||||
#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;
|
||||
|
||||
|
||||
@@ -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