#include #include #include #include "list.h" typedef struct { int hp; list_t node; list_t node_sorted; char name[1]; /* must be the last member, lager than 0. */ } champion_t; static void print_champs(const char *msg, const list_t *champs) { const list_t *node; printf("%s\n", msg); list_for_each(node, champs) { const champion_t *champ = list_entry(node, champion_t, node); printf("name:%s hp:%d\n", champ->name, champ->hp); } } static void print_champs_sorted(const char *msg, const list_t *champs) { const list_t *node; printf("%s\n", msg); list_for_each(node, champs) { const champion_t *champ = list_entry(node, champion_t, node_sorted); printf("name:%s hp:%d\n", champ->name, champ->hp); } } static champion_t *new_champ(const char *name, const int hp) { champion_t *champ = (champion_t *)malloc(sizeof(*champ) + strlen(name)); champ->hp = hp; strcpy(champ->name, name); list_init(&champ->node); list_init(&champ->node_sorted); return champ; } static void add_champ(champion_t *champ, list_t *list) { list_add(&champ->node, list); } static void del_champ(champion_t *champ) { list_del(&champ->node); } static void destroy_champs(list_t *champs) { while (!list_is_empty(champs)) { champion_t *champ = list_entry(champs->next, champion_t, node); del_champ(champ); free(champ); } } static void sort_champs(list_t *org, list_t *sorted, int (*cmp)(const void *, const void *)) { list_t *node, *node_s, *here; champion_t *champ, *champ_s; list_for_each(node, org) { champ = list_entry(node, champion_t, node); here = sorted; list_for_each(node_s, sorted) { champ_s = list_entry(node_s, champion_t, node_sorted); if (cmp(champ, champ_s) >= 0) { here = node_s; break; } } list_add(&champ->node_sorted, here); } } static int compare_hp(const void *_a, const void *_b) { const champion_t *a = (const champion_t *)_a; const champion_t *b = (const champion_t *)_b; return a->hp - b->hp; } static int compare_name(const void *_a, const void *_b) { const champion_t *a = (const champion_t *)_a; const champion_t *b = (const champion_t *)_b; return strcmp(a->name, b->name); } int main(void) { list_t champions; list_t champions_sorted; list_init(&champions); add_champ(new_champ("abc", 10), &champions); add_champ(new_champ("ghi", 5), &champions); add_champ(new_champ("def", 20), &champions); print_champs("--list--", &champions); list_init(&champions_sorted); sort_champs(&champions, &champions_sorted, compare_hp); print_champs_sorted("--by hp--", &champions_sorted); list_init(&champions_sorted); sort_champs(&champions, &champions_sorted, compare_name); print_champs_sorted("--by name--", &champions_sorted); destroy_champs(&champions); return 0; }