Class to manage a linked list. Utility that can be built on or used alone
Dependents: Waldo_Embed_V2 elevator_with_queue RaheeNew DS1820 ... more
Good information on linked list basics here.
#include "mbed.h" #include "LL.h" LL<node>list; int main() { node *tmp; list.push((char *)"Two\n"); list.append((char *)"Three\n"); list.append((char *)"Four\n"); list.push((char*)"One\n"); list.append((char*)"Five\n"); for(int i=1; i<=list.length(); i++) { tmp = list.pop(i); printf("%s", (char *)tmp->data ); } error("done\n"); }
Diff: LinkedList.cpp
- Revision:
- 3:c14e7a918e21
- Parent:
- 1:a032c0392ba1
- Child:
- 4:59b2aa82b517
--- a/LinkedList.cpp Fri Apr 05 23:44:10 2013 +0000 +++ b/LinkedList.cpp Mon Apr 08 22:23:25 2013 +0000 @@ -133,17 +133,17 @@ retT *current = _head; retT *prev = 0; // make sure we have an item to remove - if (loc < length()) + if ((loc < length()) && (loc > 0)) { // move to the item we want to delete - if (0 == loc) + if (1 == loc) { _head = current->next; delete [] current; } else { - for (uint32_t i=0; i<loc; ++i) + for (uint32_t i=2; i<=loc; ++i) { prev = current; current = current->next; @@ -162,12 +162,12 @@ { retT *current = _head; // make sure we have something in the location - if (loc > length()) + if ((loc > length()) || (loc == 0)) { return 0; } // and if so jump down the list - for (uint32_t i=0; i<loc; ++i) + for (uint32_t i=2; i<=loc; ++i) { current = current->next; } @@ -178,12 +178,12 @@ template<class retT> uint32_t LinkedList<retT>::length(void) { - int count = 0; + int32_t count = 0; retT *current = _head; //loop until the end of the list is found while (current != 0) { - count++; + ++count; current = current->next; }