00001 #include <stdlib.h> 00002 #include <string.h> 00003 #include "list.h" 00004 00005 list *make_list() 00006 { 00007 list *l = malloc(sizeof(list)); 00008 l->size = 0; 00009 l->front = 0; 00010 l->back = 0; 00011 return l; 00012 } 00013 00014 /* 00015 void transfer_node(list *s, list *d, node *n) 00016 { 00017 node *prev, *next; 00018 prev = n->prev; 00019 next = n->next; 00020 if(prev) prev->next = next; 00021 if(next) next->prev = prev; 00022 --s->size; 00023 if(s->front == n) s->front = next; 00024 if(s->back == n) s->back = prev; 00025 } 00026 */ 00027 00028 void *list_pop(list *l){ 00029 if(!l->back) return 0; 00030 node *b = l->back; 00031 void *val = b->val; 00032 l->back = b->prev; 00033 if(l->back) l->back->next = 0; 00034 free(b); 00035 --l->size; 00036 00037 return val; 00038 } 00039 00040 void list_insert(list *l, void *val) 00041 { 00042 node *new = malloc(sizeof(node)); 00043 new->val = val; 00044 new->next = 0; 00045 00046 if(!l->back){ 00047 l->front = new; 00048 new->prev = 0; 00049 }else{ 00050 l->back->next = new; 00051 new->prev = l->back; 00052 } 00053 l->back = new; 00054 ++l->size; 00055 } 00056 00057 void free_node(node *n) 00058 { 00059 node *next; 00060 while(n) { 00061 next = n->next; 00062 free(n); 00063 n = next; 00064 } 00065 } 00066 00067 void free_list(list *l) 00068 { 00069 free_node(l->front); 00070 free(l); 00071 } 00072 00073 void free_list_contents(list *l) 00074 { 00075 node *n = l->front; 00076 while(n){ 00077 free(n->val); 00078 n = n->next; 00079 } 00080 } 00081 00082 void **list_to_array(list *l) 00083 { 00084 void **a = calloc(l->size, sizeof(void*)); 00085 int count = 0; 00086 node *n = l->front; 00087 while(n){ 00088 a[count++] = n->val; 00089 n = n->next; 00090 } 00091 return a; 00092 }