lits_t.c (1005B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #include "../list.h" 5 6 struct Item { 7 int id; 8 }; 9 10 void iprint(struct Item *); 11 struct Item* icreate(int); 12 void ifree(struct Item *); 13 int icmp(struct Item *, struct Item *); 14 15 void 16 iprint(struct Item *p) 17 { 18 printf("[%d]", p->id); 19 } 20 21 struct Item* 22 icreate(int id) 23 { 24 struct Item *p; 25 p = (struct Item *)malloc(sizeof(struct Item)); 26 p->id = id; 27 return p; 28 } 29 30 void 31 ifree(struct Item *p) 32 { 33 free(p); 34 } 35 36 int 37 icmp(struct Item *p, struct Item *q) 38 { 39 if (p->id < q->id) 40 return -1; 41 else if (p->id == q->id) 42 return 0; 43 else 44 return 1; 45 } 46 47 int 48 main(void) 49 { 50 struct List *p = NULL, *q = NULL; 51 52 for (int i = 0; i < 10; i++) { 53 if ((q = laddfront(p, icreate(random()%100))) == NULL) { 54 // Maybe this is not the error of laddfront. 55 fprintf(stderr, "addfront error.\n"); 56 } else { 57 p = q; 58 } 59 } 60 lprint(p, (void (*)(void *))&iprint); 61 lqsort(p, (int (*)(void *, void *))&icmp); 62 lprint(p, (void (*)(void *))&iprint); 63 lfreei(p, (void (*)(void *))&ifree); 64 65 return 0; 66 }