#include "node.hpp" #include "defs.hpp" #include "memory.hpp" /******************************************************************************* ** NodeCreate *******************************************************************************/ void NodeCreate(Node* node) { memset(node, 0, sizeof(Node)); } /******************************************************************************* ** NodeLink *******************************************************************************/ void NodeLink(Node* n1, Node* n2) { if (n1) { if (n1->next) n1->next->prev = NULL; n1->next = n2; } if (n2) { if (n2->prev) n2->prev->next = NULL; n2->prev = n1; } } /******************************************************************************* ** NodePull *******************************************************************************/ Node* NodePull(Node* node) { NodeLink(node->prev, node->next); return node; } /******************************************************************************* ** NodePush *******************************************************************************/ Node* NodePush(Node* node, Node* prev, Node* next) { NodePull(node); NodeLink(prev, node); NodeLink(node, next); return node; } /******************************************************************************* ** DataPull *******************************************************************************/ void* DataPull(Node* node) { void* data = node->data; NodeLink(node->prev, node->next); free(node); return data; } /******************************************************************************* ** DataPush *******************************************************************************/ void* DataPush(void* data, Node* prev, Node* next) { Node* node = (Node*)calloc(1, sizeof(Node)); assert(node && data); node->data = data; NodeLink(prev, node); NodeLink(node, next); return data; } /******************************************************************************* ** ListCreate *******************************************************************************/ List* ListCreate(void) { List* list = (List*) calloc(1, sizeof(List)); if (list) NodeLink(&list->header, &list->footer); return list; } /******************************************************************************* ** ListIsEmpty *******************************************************************************/ int ListIsEmpty(List* list) { return !(list && list->header.next && list->header.next->next); } /******************************************************************************* ** ListDelete *******************************************************************************/ void ListDelete(List* list) { assert(!ListIsEmpty(list)); free(list); } /******************************************************************************* ** ListHead *******************************************************************************/ Node* ListHead(List* list) { assert(!ListIsEmpty(list)); return list->header.next; } /******************************************************************************* ** ListTail *******************************************************************************/ Node* ListTail(List* list) { assert(!ListIsEmpty(list)); return list->footer.prev; } /******************************************************************************* ** ListPullHead *******************************************************************************/ Node* ListPullHead(List* list) { return NodePull(ListHead(list)); } /******************************************************************************* ** ListPullTail *******************************************************************************/ Node* ListPullTail(List* list) { return NodePull(ListTail(list)); } /******************************************************************************* ** ListPushHead *******************************************************************************/ Node* ListPushHead(List* list, Node* node) { return NodePush(node, &list->header, list->header.next); } /******************************************************************************* ** ListPushTail *******************************************************************************/ Node* ListPushTail(List* list, Node* node) { return NodePush(node, list->footer.prev, &list->footer); } /******************************************************************************* ** DataPullHead *******************************************************************************/ void* DataPullHead(List* list) { return DataPull(ListHead(list)); } /******************************************************************************* ** DataPullTail *******************************************************************************/ void* DataPullTail(List* list) { return DataPull(ListTail(list)); } /******************************************************************************* ** DataPushHead *******************************************************************************/ void* DataPushHead(List* list, void* data) { return DataPush(data, &list->header, list->header.next); } /******************************************************************************* ** DataPushTail *******************************************************************************/ void* DataPushTail(List* list, void* data) { return DataPush(data, list->footer.prev, &list->footer); }