Work on music, midi, and seq interfaces.
[cantaveria.git] / list.c
blobf103b365e5b3165f867995624e823d878ec1bfb0
1 #include <stdlib.h>
2 #include <stdio.h>
5 #include <list.h>
7 list* freelist = NULL;
10 list* make_new(list* next, void* item){
11 list* ptr;
13 if(freelist == NULL){
14 ptr = malloc(sizeof(struct list));
16 else{
17 ptr = freelist;
18 freelist = ptr->next;
21 ptr->next = next;
22 ptr->item = item;
23 return ptr;
26 void remove_from_list(list* prev, list* node){
27 prev->next = node->next;
28 node->item = NULL;
29 node->next = freelist;
30 freelist = node;
34 list* empty(){
35 return make_new(NULL, NULL);
38 void push(list* L, void* item){
39 list* ptr = make_new(L->next, item);
40 L->next = ptr;
43 void* pop(list* L){
44 if(L->next){
45 void* item = L->next->item;
46 remove_from_list(L, L->next);
47 return item;
49 else{
50 return NULL;
54 void append(list* L, void* item){
55 list* ptr = L;
56 while(ptr->next){
57 ptr = ptr->next;
59 ptr->next = make_new(NULL, item);
62 void recycle(list* L){
63 list* ptr = L;
64 while(ptr->next){
65 ptr->next->item = NULL;
66 ptr = ptr->next;
68 ptr->next = freelist;
69 freelist = L;
72 int length(list* L){
73 list* ptr = L->next;
74 int c = 0;
75 while(ptr){
76 c += 1;
77 ptr = ptr->next;
79 return c;
85 void print_list(list* L, void (*print)(void* item)){
86 list* ptr = L->next;
87 printf("(");
88 while(ptr){
89 print(ptr->item);
90 if(ptr->next)
91 printf(", ");
92 ptr = ptr->next;
94 printf(")");
97 void println_list(list* L, void (*print)(void* item)){
98 print_list(L, print);
99 printf("\n");
102 void list_print_free(){
103 list* ptr = freelist;
104 printf("(");
105 while(ptr){
106 printf("_");
107 if(ptr->next){
108 printf(", ");
110 ptr = ptr->next;
112 printf(")\n");
115 void print(void* item){
116 char* s = item;
117 printf("%s", s);