int main(void) { struct score tmp, *p; LLIST *list; int i; list = llist_creat(sizeof(struct score)); /* if error */ for (i = 0; i < 9; i++) { tmp.id = i; tmp.ch = 100 - i; tmp.math = 100 - i * 2; tmp.en = 100 - i * 3; snprintf(tmp.name, NAMESIZE, "stu%d", i); llist_add(list, &tmp, LLIST_ADD_FORWARD); } llist_travel(list, print_score); llist_delet(list, "stu6", name_cmp); printf("\n"); llist_travel(list, print_score); #if 0 p = llist_find(list, "stuxx", name_cmp); if (p == NULL) { printf("Can not find.\n"); } else { print_score(p); } #endif llist_destroy(list); return 0; }
//变长结构体版本 //类的概念:数据和方法都放入头节点 int main(int argc, const char *argv[]) { int i, ret; LLIST *handler; SCORE_ST tmp, *tmp_p; handler = llist_creat(sizeof(SCORE_ST)); for (i = 0; i < 7; i++) { tmp.id = i; snprintf(tmp.name, NAMESIZE, "middle school student%d", i); tmp.math = rand() %100; tmp.chinese = rand() %100; //myprint(&tmp); llist_insert(handler, &tmp, LLIST_FORWARD); } llist_travel(handler, myprint); printf("-----------------------------------\n"); char *name = "stu3"; tmp_p = llist_find(handler, name, cmp_name); if (tmp_p) { myprint(tmp_p); }else{ printf("not find\n"); } printf("-----------------------------------\n"); int id = 2; ret = llist_fetch(handler, &id, cmp_id, &tmp); if (ret) { printf("fetch "); myprint(&tmp); } printf("-----------------------------------\n"); name ="stu0"; ret = llist_delete(handler, name, cmp_name); if (ret) { printf("delete %s\n", name); } printf("-----------------------------------\n"); llist_travel(handler, myprint); printf("-----------------------------------\n"); name = malloc(NAMESIZE); for (i = 1; i < 7; i++) { snprintf(name, NAMESIZE, "stu%d", i); ret = llist_delete(handler, name, cmp_name); if (ret) { printf("delete %s\n", name); } } llist_travel(handler, myprint); llist_destroy(handler); return 0; }
int init(void) { int i; for (i = 0; i < LIST_NR; i++) { list[i] = llist_creat(sizeof(struct score)); /* if error */ } }
int main(void) { struct score tmp, *datap; int i, id; LLIST *list; int ret; list = llist_creat(sizeof(struct score)); for (i = 0; i < 7; i++) { tmp.id = i; tmp.math = 100 - i; snprintf(tmp.name, NAMESIZE, "stu%d", i); llist_insert(list, &tmp, LLIST_BACKWARD); } llist_travel(list, print_s); id = 4; ret = llist_fetch(list, &tmp, &id, id_cmp); printf("\n"); if (ret == -1) { printf("Can not find.\n"); } else { print_s(&tmp); } printf("\n"); llist_travel(list, print_s); #if 0 id = 8; llist_delet(list, &id, id_cmp); printf("\n"); llist_travel(list, print_s); id = 2; datap = llist_find(list, &id, id_cmp); if (datap == NULL) { printf("Can not find.\n"); } else { print_s(datap); } #endif llist_save(list, "stu.data"); llist_destroy(list); LLIST *list2 = llist_load("stu.data"); llist_travel(list2, print_s); llist_destroy(list2); return 0; }
int main(void) { LLIST *llist; struct score tmp, *datap; int i; int id; struct sum_st sum = {0,0}; llist = llist_creat(sizeof(struct score)); /* if error */ for (i = 0; i < 7; i++) { tmp.id = i; snprintf(tmp.name, NAMESIZE, "stu%d", i); tmp.ch = 100 - i; tmp.math = 100 - 2 * i; llist_insert(llist, &tmp); /* if error */ } llist_travel(llist, print_score, NULL); printf("\n"); #if 1 id = 3; llist_delet(llist, &id, id_cmp); llist_travel(llist, print_score, NULL); #else datap = llist_find(llist, &id, id_cmp); if (datap == NULL) { printf("Can not find.\n"); } else { print_score(datap, NULL); } llist_travel(llist, get_sum, &sum); printf("sum: ch = %d, math = %d\n", sum.ch, sum.math); #endif llist_destroy(llist); return 0; }
STACK *stack_creat(int size) { return llist_creat(size); }