Added ability to maintain ordered linked list based on "insertAsc" function. The function takes a comparator function that allows for specific order behavior. If values collide, then FIFO or LIFO order can be maintained based on comparator function implementation.
Fork of LinkedList by
Diff: LinkedList.h
- Revision:
- 14:7fd12867824f
- Parent:
- 13:b06f7b37fff4
- Child:
- 15:f715e3067fa8
--- a/LinkedList.h Thu Jul 13 20:00:54 2017 +0000 +++ b/LinkedList.h Thu Jul 13 23:59:55 2017 +0000 @@ -26,6 +26,14 @@ #include <stdint.h> #include "mbed.h" +#define DBG_LLIST 0 + +#if DBG_LLIST +#define DBG(...) do{debug("[%s:%d]", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0); +#else +#define DBG(...) while(0); +#endif + /** * @struct node * @brief The Linked List structure @@ -329,7 +337,7 @@ if (0 == _head) { _head = new_node; - //printf("[insertAsc] set as head\n"); + DBG("[insertAsc] set as head\n"); return new_node; } @@ -342,7 +350,7 @@ // data being inserted is smaller than current->data if (0 == current->next) { // there is no more data - //printf("[insertAsc] insert at end\n"); + DBG("[insertAsc] insert at end\n"); current->next = new_node; return new_node; } @@ -350,12 +358,12 @@ current = current->next; } if (0 == prev) { - //printf("[insertAsc] insert at start\n"); + DBG("[insertAsc] insert at start\n"); new_node->next = _head; _head = new_node; return new_node; } - //printf("[insertAsc] insert inside\n"); + DBG("[insertAsc] insert inside\n"); new_node->next = current; prev->next = new_node; return new_node; @@ -454,18 +462,23 @@ node<T> *pop(T* data, bool (isEqual)(T* data1, T* data2)) { + DBG("..pop started\n"); if (_head == NULL) { + DBG(".._head is NULL\n"); return NULL; } node<T> *current = _head; // make sure we have something in the location // and if so jump down the list while(!isEqual(current->data, data)) { - current = current->next; + DBG("..next current\n"); if (current->next == NULL) { + DBG("..next is NULL; returning NULL\n"); return NULL; } + current = current->next; } + DBG("..found\n"); return current; }