1 /* 2 This is part of pyahocorasick Python module. 3 4 Linked list implementation. 5 6 Const time of: 7 * append 8 * prepend 9 * pop first 10 * get first/last 11 12 Author : Wojciech Muła, wojciech_mula@poczta.onet.pl 13 WWW : http://0x80.pl 14 License : public domain 15 */ 16 #include "slist.h" 17 18 ListItem* list_item_new(const size_t size)19list_item_new(const size_t size) { 20 ListItem* item = (ListItem*)memory_alloc(size); 21 if (item) { 22 item->__next = 0; 23 } 24 25 return item; 26 } 27 28 29 void list_item_delete(ListItem * item)30list_item_delete(ListItem* item) { 31 memory_free(item); 32 } 33 34 35 void list_init(List * list)36list_init(List* list) { 37 if (list) { 38 list->head = 0; 39 list->last = 0; 40 } 41 } 42 43 44 int list_delete(List * list)45list_delete(List* list) { 46 47 ListItem* item; 48 ListItem* tmp; 49 50 ASSERT(list); 51 52 item = list->head; 53 while (item) { 54 tmp = item; 55 item = item->__next; 56 memory_free(tmp); 57 } 58 59 list->head = list->last = NULL; 60 return 0; 61 } 62 63 64 ListItem* list_append(List * list,ListItem * item)65list_append(List* list, ListItem* item) { 66 ASSERT(list); 67 68 if (item) { 69 if (list->last) { 70 list->last->__next = item; // append 71 list->last = item; // set as last node 72 } 73 else 74 list->head = list->last = item; 75 } 76 77 return item; 78 } 79 80 81 ListItem* list_push_front(List * list,ListItem * item)82list_push_front(List* list, ListItem* item) { 83 ASSERT(list); 84 85 if (list->head) { 86 item->__next = list->head; 87 list->head = item; 88 } 89 else 90 list->head = list->last = item; 91 92 return item; 93 } 94 95 96 ListItem* list_pop_first(List * list)97list_pop_first(List* list) { 98 ListItem* item; 99 100 ASSERT(list); 101 102 if (list->head) { 103 item = list->head; 104 list->head = item->__next; 105 106 if (!list->head) 107 list->last = 0; 108 109 return item; 110 } 111 else 112 return NULL; 113 } 114 115