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.

Information

Dependencies not included with library:

#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");
}

Files at this revision

API Documentation at this revision

Comitter:
sam_grove
Date:
Mon Apr 08 22:23:25 2013 +0000
Parent:
2:704e1c9057c1
Child:
4:59b2aa82b517
Commit message:
Updated class to offset such that 0 is empty or not a valid location. The start of the list logic is 1. Added documentation

Changed in this revision

LinkedList.cpp Show annotated file Show diff for this revision Revisions of this file
LinkedList.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
     }
 
--- a/LinkedList.h	Fri Apr 05 23:44:10 2013 +0000
+++ b/LinkedList.h	Mon Apr 08 22:23:25 2013 +0000
@@ -52,7 +52,7 @@
  *      list.push((char*)"One\n");
  *      list.append((char*)"Five\n");
  *      
- *      for(int i=0; i<list.length(); i++)
+ *      for(int i=1; i<=list.length(); i++)
  *      {
  *          tmp = list.pop(i);
  *          printf("%s", (char *)tmp->data);