#ifndef _UTIL_LINKED_LIST_H #define _UTIL_LINKED_LIST_H #include #include template struct LinkedList { T d; struct LinkedList* prev; struct LinkedList* next; }; template bool linked_list_append(struct LinkedList*& list, T* data = nullptr, struct LinkedList** tail = nullptr) { bool have_list = list; struct LinkedList* tmp = (struct LinkedList*)malloc(sizeof(LinkedList)); if (!tmp) return false; memset(tmp, 0, sizeof(LinkedList)); if (data) tmp->d = T(*data); else tmp->d = T(); if (!have_list) { list = tmp; } else { struct LinkedList* t = list; while (t->next) t = t->next; t->next = tmp; tmp->prev = t; } if (tail) *tail = tmp; return true; } template bool linked_list_append(struct LinkedList*& list, struct LinkedList** tail) { return linked_list_append(list, (T*)nullptr, tail); } template void linked_list_clear(struct LinkedList*& list, void(*free_func)(T) = nullptr) { if (!list) return; struct LinkedList* t = list; struct LinkedList* tmp = nullptr; if (free_func) free_func(t->d); while (t->next) { tmp = t; t = t->next; free(tmp); if (free_func) free_func(t->d); } free(t); list = nullptr; } #endif