🌈 style:

This commit is contained in:
lzy
2024-05-26 15:39:14 +08:00
parent 6f28a3384a
commit a57bbdb4c1
156 changed files with 765 additions and 770 deletions

View File

@@ -3,17 +3,17 @@
#include "sqlist.h"
int main()
int main( )
{
sqlist *list = NULL;
sqlist *list1 = NULL;
sqlist* list = NULL;
sqlist* list1 = NULL;
datatype arr[] = {12, 23, 34, 45, 56};
datatype arr1[] = {89, 90, 78, 67, 56, 43};
int i, err;
list = sqlist_create();
list = sqlist_create( );
// sqlist_create1(&list);
if (NULL == list)
{
@@ -21,7 +21,7 @@ int main()
exit(1);
}
list1 = sqlist_create();
list1 = sqlist_create( );
if (NULL == list1)
{
fprintf(stderr, "sqlist_create() failed!\n");

View File

@@ -3,9 +3,9 @@
#include "sqlist.h"
sqlist *sqlist_create()
sqlist* sqlist_create( )
{
sqlist *me;
sqlist* me;
me = malloc(sizeof(*me));
if (NULL == me)
@@ -16,7 +16,7 @@ sqlist *sqlist_create()
return me;
}
void sqlist_create1(sqlist **ptr)
void sqlist_create1(sqlist** ptr)
{
*ptr = malloc(sizeof(**ptr));
if (NULL == *ptr)
@@ -26,7 +26,7 @@ void sqlist_create1(sqlist **ptr)
return;
}
int sqlist_insert(sqlist *me, int i, datatype *data)
int sqlist_insert(sqlist* me, int i, datatype* data)
{
int j;
@@ -48,7 +48,7 @@ int sqlist_insert(sqlist *me, int i, datatype *data)
return 0;
}
int sqlist_delete(sqlist *me, int i)
int sqlist_delete(sqlist* me, int i)
{
int j;
@@ -65,7 +65,7 @@ int sqlist_delete(sqlist *me, int i)
return 0;
}
int sqlist_find(sqlist *me, datatype *data)
int sqlist_find(sqlist* me, datatype* data)
{
if (0 == sqlist_isempty(me))
return -1;
@@ -79,25 +79,25 @@ int sqlist_find(sqlist *me, datatype *data)
return -2;
}
int sqlist_isempty(sqlist *me)
int sqlist_isempty(sqlist* me)
{
if (-1 == me->last)
return 0;
return -1;
}
int sqlist_setempty(sqlist *me)
int sqlist_setempty(sqlist* me)
{
me->last = -1;
return 0;
}
int sqlist_getnum(sqlist *me)
int sqlist_getnum(sqlist* me)
{
return me->last + 1;
}
void sqlist_display(sqlist *me)
void sqlist_display(sqlist* me)
{
if (-1 == me->last)
return;
@@ -108,13 +108,13 @@ void sqlist_display(sqlist *me)
printf("\n");
}
int sqlist_destroy(sqlist *me)
int sqlist_destroy(sqlist* me)
{
free(me);
return 0;
}
int sqlist_union(sqlist *list1, sqlist *list2)
int sqlist_union(sqlist* list1, sqlist* list2)
{
// list1 -> 12 23 34 45 56
// list2 -> 78 89 56 23 10

View File

@@ -11,26 +11,26 @@ typedef struct node_st
int last;
} sqlist;
sqlist *sqlist_create();
void sqlist_create1(sqlist **me);
sqlist* sqlist_create( );
void sqlist_create1(sqlist** me);
int sqlist_insert(sqlist *, int i, datatype *data);
int sqlist_insert(sqlist*, int i, datatype* data);
int sqlist_delete(sqlist *, int i);
int sqlist_delete(sqlist*, int i);
int sqlist_find(sqlist *, datatype *data);
int sqlist_find(sqlist*, datatype* data);
int sqlist_isempty(sqlist *);
int sqlist_isempty(sqlist*);
int sqlist_setempty(sqlist *);
int sqlist_setempty(sqlist*);
int sqlist_getnum(sqlist *);
int sqlist_getnum(sqlist*);
void sqlist_display(sqlist *);
void sqlist_display(sqlist*);
int sqlist_destroy(sqlist *);
int sqlist_destroy(sqlist*);
int sqlist_union(sqlist *, sqlist *);
int sqlist_union(sqlist*, sqlist*);
#endif

View File

@@ -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)

View File

@@ -14,16 +14,16 @@ 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);
}
int main()
int main( )
{
int i;
struct score_st *datap;
struct list_head *cur;
struct score_st* datap;
struct list_head* cur;
LIST_HEAD(head);
@@ -35,8 +35,8 @@ int main()
datap->id = i;
snprintf(datap->name, NAMESIZE, "stu%d", i);
datap->math = rand() % 100;
datap->chinese = rand() % 100;
datap->math = rand( ) % 100;
datap->chinese = rand( ) % 100;
list_add(&datap->node, &head);
}

View File

@@ -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;

View File

@@ -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

View File

@@ -14,33 +14,33 @@ 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);
}
int main()
int main( )
{
LLIST *handler;
LLIST* handler;
int i, ret;
struct score_st tmp;
@@ -52,8 +52,8 @@ int main()
{
tmp.id = i;
snprintf(tmp.name, NAMESIZE, "std%d", i);
tmp.math = rand() % 100;
tmp.chinese = rand() % 100;
tmp.math = rand( ) % 100;
tmp.chinese = rand( ) % 100;
ret = llist_insert(handler, &tmp, LLIST_FORWARD);
if (ret)
@@ -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);

View File

@@ -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;

View File

@@ -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

View File

@@ -14,33 +14,33 @@ 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);
}
int main()
int main( )
{
LLIST *handler;
LLIST* handler;
int i, ret;
struct score_st tmp;
@@ -52,8 +52,8 @@ int main()
{
tmp.id = i;
snprintf(tmp.name, NAMESIZE, "std%d", i);
tmp.math = rand() % 100;
tmp.chinese = rand() % 100;
tmp.math = rand( ) % 100;
tmp.chinese = rand( ) % 100;
ret = llist_insert(handler, &tmp, LLIST_FORWARD);
if (ret)
@@ -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);

View File

@@ -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;

View File

@@ -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

View File

@@ -14,33 +14,33 @@ 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);
}
int main()
int main( )
{
LLIST *handler;
LLIST* handler;
int i, ret;
struct score_st tmp;
@@ -52,8 +52,8 @@ int main()
{
tmp.id = i;
snprintf(tmp.name, NAMESIZE, "std%d", i);
tmp.math = rand() % 100;
tmp.chinese = rand() % 100;
tmp.math = rand( ) % 100;
tmp.chinese = rand( ) % 100;
ret = handler->insert(handler, &tmp, LLIST_FORWARD);
if (ret)

View File

@@ -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)

View File

@@ -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

View File

@@ -14,33 +14,33 @@ 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);
}
int main()
int main( )
{
LLIST *handler;
LLIST* handler;
int i, ret;
struct score_st tmp;
@@ -52,8 +52,8 @@ int main()
{
tmp.id = i;
snprintf(tmp.name, NAMESIZE, "std%d", i);
tmp.math = rand() % 100;
tmp.chinese = rand() % 100;
tmp.math = rand( ) % 100;
tmp.chinese = rand( ) % 100;
ret = llist_insert(handler, &tmp, LLIST_FORWARD);
if (ret)
@@ -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);

View File

@@ -3,9 +3,9 @@
#include "list.h"
list *list_create()
list* list_create( )
{
list *me;
list* me;
me = malloc(sizeof(*me));
if (NULL == me)
return NULL;
@@ -16,7 +16,7 @@ list *list_create()
}
// 从第一个有效节点作为第0个
int list_insert_at(list *me, int i, datatype *data)
int list_insert_at(list* me, int i, datatype* data)
{
int k = 0;
list *node = me, *newnode;
@@ -50,7 +50,7 @@ int list_insert_at(list *me, int i, datatype *data)
}
// 有序插入
int list_order_insert(list *me, datatype *data)
int list_order_insert(list* me, datatype* data)
{
list *p = me, *q;
@@ -68,7 +68,7 @@ int list_order_insert(list *me, datatype *data)
return 0;
}
int list_delete_at(list *me, int i, datatype *data)
int list_delete_at(list* me, int i, datatype* data)
{
int k = 0;
list *p = me, *q;
@@ -98,7 +98,7 @@ int list_delete_at(list *me, int i, datatype *data)
return -2;
}
int list_delete(list *me, datatype *data)
int list_delete(list* me, datatype* data)
{
list *p = me, *q;
while (p->next && p->next->data != *data)
@@ -117,16 +117,16 @@ int list_delete(list *me, datatype *data)
return 0;
}
int list_isempty(list *me)
int list_isempty(list* me)
{
if (NULL == me->next)
return 0;
return 1;
}
void list_display(list *me)
void list_display(list* me)
{
list *node = me->next;
list* node = me->next;
if (0 == list_isempty(me))
return;
@@ -140,7 +140,7 @@ void list_display(list *me)
return;
}
void list_destroy(list *me)
void list_destroy(list* me)
{
list *node, *next;

View File

@@ -5,22 +5,22 @@ typedef int datatype;
typedef struct node_st
{
datatype data;
struct node_st *next;
}list;
datatype data;
struct node_st* next;
} list;
list *list_create();
list* list_create( );
int list_insert_at(list *me, int i, datatype *data);
int list_order_insert(list *me, datatype *data);
int list_insert_at(list* me, int i, datatype* data);
int list_order_insert(list* me, datatype* data);
int list_delete_at(list *me, int i, datatype *data);
int list_delete(list *me, datatype *data);
int list_delete_at(list* me, int i, datatype* data);
int list_delete(list* me, datatype* data);
int list_isempty(list *me);
int list_isempty(list* me);
void list_display(list *me);
void list_destroy(list *me);
void list_display(list* me);
void list_destroy(list* me);
#endif

View File

@@ -3,12 +3,12 @@
#include "list.h"
int main()
int main( )
{
list *l;
list* l;
datatype arr[] = {12, 9, 23, 2, 34, 6, 45};
l = list_create();
l = list_create( );
if (NULL == l)
exit(1);

View File

@@ -6,7 +6,7 @@
typedef struct node_st
{
int data;
struct node_st *next;
struct node_st* next;
} list;
// try something
@@ -32,7 +32,7 @@ list *jose_create(int a[], int n)
}
#endif
list *jose_create(int n)
list* jose_create(int n)
{
list *me, *newnode, *cur;
int i = 1;
@@ -61,9 +61,9 @@ list *jose_create(int n)
return me;
}
void jose_show(list *me)
void jose_show(list* me)
{
list *list;
list* list;
for (list = me; list->next != me; list = list->next)
{
@@ -74,7 +74,7 @@ void jose_show(list *me)
printf("%d\n", list->data);
}
void jose_kill(list **me, int n)
void jose_kill(list** me, int n)
{
list *cur = *me, *node;
int i = 1;
@@ -101,9 +101,9 @@ void jose_kill(list **me, int n)
printf("\n");
}
int main()
int main( )
{
list *list;
list* list;
list = jose_create(JOSE_NUM);

View File

@@ -3,18 +3,18 @@
#include "nohead.h"
int main()
int main( )
{
int i, ret;
struct node_st *list = NULL;
struct node_st* list = NULL;
struct score_st tmp;
for (i = 0; i < 7; i++)
{
tmp.id = i;
snprintf(tmp.name, NAMESIZE, "stu%d", i);
tmp.math = rand() % 100;
tmp.chinese = rand() % 100;
tmp.math = rand( ) % 100;
tmp.chinese = rand( ) % 100;
// list = list_insert(list, &tmp);
ret = list_insert(&list, &tmp);
@@ -28,7 +28,7 @@ int main()
printf("\n\n");
int id = 13;
struct score_st *ptr;
struct score_st* ptr;
ptr = list_find(&list, id);
if (NULL == ptr)
printf("Can not find!\n");

View File

@@ -21,9 +21,9 @@ struct node_st *list_insert(struct node_st *list, struct score_st *data)
}
#endif
int list_insert(struct node_st **list, struct score_st *data)
int list_insert(struct node_st** list, struct score_st* data)
{
struct node_st *new;
struct node_st* new;
new = malloc(sizeof(*new));
if (NULL == new)
@@ -36,9 +36,9 @@ int list_insert(struct node_st **list, struct score_st *data)
return 0;
}
void list_show(struct node_st *list)
void list_show(struct node_st* list)
{
struct node_st *cur;
struct node_st* cur;
for (cur = list; NULL != cur; cur = cur->next)
{
@@ -51,9 +51,9 @@ void list_show(struct node_st *list)
}
// 删除首位
int list_delete(struct node_st **list)
int list_delete(struct node_st** list)
{
struct node_st *cur;
struct node_st* cur;
if (NULL == *list)
return -1;
@@ -66,9 +66,9 @@ int list_delete(struct node_st **list)
return 0;
}
struct score_st *list_find(struct node_st **list, int id)
struct score_st* list_find(struct node_st** list, int id)
{
struct node_st *cur;
struct node_st* cur;
if (NULL == *list)
return NULL;
@@ -91,9 +91,9 @@ struct score_st *list_find(struct node_st **list, int id)
return NULL;
}
int list_destroy(struct node_st **list)
int list_destroy(struct node_st** list)
{
struct node_st *cur;
struct node_st* cur;
if (NULL == list)
return -1;

View File

@@ -11,16 +11,16 @@ struct score_st
struct node_st
{
struct score_st data;
struct node_st *next;
struct node_st* next;
};
// struct node_st *list_insert(struct node_st *list, struct score_st *data);
int list_insert(struct node_st **list, struct score_st *data);
int list_insert(struct node_st** list, struct score_st* data);
void list_show(struct node_st *list);
void list_show(struct node_st* list);
int list_delete(struct node_st **list);
int list_delete(struct node_st** list);
struct score_st *list_find(struct node_st **list, int id);
struct score_st* list_find(struct node_st** list, int id);
int list_destroy(struct node_st **list);
int list_destroy(struct node_st** list);

View File

@@ -5,10 +5,10 @@ typedef struct node_st
{
int coef;
int exp;
struct node_st *next;
struct node_st* next;
} list;
list *poly_create(int a[][2], int n)
list* poly_create(int a[][2], int n)
{
list *me, *newnode, *cur;
@@ -36,9 +36,9 @@ list *poly_create(int a[][2], int n)
return me;
}
void poly_show(list *me)
void poly_show(list* me)
{
list *cur;
list* cur;
for (cur = me->next; NULL != cur; cur = cur->next)
{
@@ -47,7 +47,7 @@ void poly_show(list *me)
printf("\n");
}
void poly_union(list *p1, list *p2)
void poly_union(list* p1, list* p2)
{
list *p, *q, *r;
@@ -89,7 +89,7 @@ void poly_union(list *p1, list *p2)
r->next = p;
}
int main()
int main( )
{
int a[][2] = {{5, 0}, {2, 1}, {8, 8}, {3, 16}};
int b[][2] = {{6, 1}, {16, 6}, {-8, 8}};