Created
October 1, 2014 04:37
-
-
Save ivan-krukov/675db290232fdd0f20ed to your computer and use it in GitHub Desktop.
Some queue-like lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "list.h" | |
list* list_init() { | |
list* ls = (list*)malloc(sizeof(list)); | |
ls->head = (list_node*)malloc(sizeof(list_node)); | |
ls->cursor = ls->head; | |
ls->head->data = NULL; | |
ls->dealloc = NULL; | |
ls->copy = NULL; | |
return ls; | |
} | |
list_node* list_append(list* ls, void* data, size_t el_size) { | |
list_node* n = (list_node*)malloc(sizeof(list_node)); | |
if (ls->copy) { | |
ls->copy(n->data,data); | |
} else { | |
n->data = LIST_MALLOC(el_size+1); | |
memcpy(n->data,data,el_size); | |
*(n->data+el_size)='\0'; | |
} | |
n->next = NULL; | |
ls->cursor->next = n; | |
ls->cursor = n; | |
return n; | |
} | |
void list_dealloc(list* ls) { | |
list_node* n = ls->head; | |
while(n) { | |
list_node* temp = n->next; | |
if (ls->dealloc && n->data) { | |
ls->dealloc(n->data); | |
} else { | |
LIST_FREE(n->data); | |
} | |
LIST_FREE(n); | |
n = temp; | |
} | |
LIST_FREE(&ls->head); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment