10 list
* make_new(list
* next
, void* item
){
14 ptr
= malloc(sizeof(struct list
));
16 fprintf(stderr
, "list: OUT OF MEMORY");
30 void remove_from_list(list
* prev
, list
* node
){
31 prev
->next
= node
->next
;
33 node
->next
= freelist
;
39 return make_new(NULL
, NULL
);
42 void push(list
* L
, void* item
){
43 list
* ptr
= make_new(L
->next
, item
);
49 void* item
= L
->next
->item
;
50 remove_from_list(L
, L
->next
);
58 void append(list
* L
, void* item
){
63 ptr
->next
= make_new(NULL
, item
);
66 void recycle(list
* L
){
69 ptr
->next
->item
= NULL
;
88 static void split(list
* L
, list
** left
, list
** right
){
115 static list
* merge(list
* left
, list
* right
, compare_func cmp
){
116 /* merges right into left */
120 if(left
== NULL
) return right
;
122 if(right
&& cmp(right
->item
, left
->item
) > 0){
130 while(right
&& ptr
->next
){
131 if(cmp(right
->item
, ptr
->next
->item
) > 0){
134 tmp
->next
= ptr
->next
;
143 if(ptr
->next
== NULL
)
149 static list
* merge_sort(list
* L
, compare_func cmp
){
153 if(L
== NULL
) return NULL
;
154 if(L
->next
== NULL
) return L
;
156 split(L
, &left
, &right
);
159 merge_sort(left
, cmp
),
160 merge_sort(right
, cmp
),
165 void sort(list
* L
, compare_func cmp
){
166 L
->next
= merge_sort(L
->next
, cmp
);
171 void print_list(list
* L
, void (*print
)(void* item
)){
183 void println_list(list
* L
, void (*print
)(void* item
)){
184 print_list(L
, print
);
188 void list_print_free(){
189 list
* ptr
= freelist
;
201 void print(void* item
){