Fork of Sam Grove's Linked list library OS6 compliant.

Dependents:   DS1820

Files at this revision

API Documentation at this revision

Comitter:
star297
Date:
Tue Dec 29 13:07:51 2020 +0000
Commit message:
Initial commit

Changed in this revision

LinkedList2.cpp Show annotated file Show diff for this revision Revisions of this file
LinkedList2.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 27649dfdde4c LinkedList2.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LinkedList2.cpp	Tue Dec 29 13:07:51 2020 +0000
@@ -0,0 +1,114 @@
+
+#include "LinkedList2.h"
+
+template<class retT>
+LinkedList2<retT>::LinkedList2()
+{
+    _head = 0;
+    return;
+}
+
+template<class retT>
+LinkedList2<retT>::~LinkedList2()
+{
+    while(remove(1) != NULL);
+    return;
+}
+
+template<class retT>
+retT *LinkedList2<retT>::push(void *data)
+{
+    retT *new_node = new retT [1];
+    if (0 == new_node)
+    {
+        error("Memory allocation failed\n");
+    }
+    new_node->next = _head;
+    new_node->data = data;
+    _head = new_node;
+    return _head;
+}
+
+template<class retT>
+retT *LinkedList2<retT>::append(void *data)
+{
+    retT *current = _head;
+    retT *new_node = new retT [1];
+    if (0 == new_node)
+    {
+        error("Memory allocation failed\n");
+    }
+    new_node->data = data;
+    new_node->next = 0;
+    if (0 == current)
+    {
+        _head = new_node;
+        return _head;
+    }
+    else
+    {
+        while (current->next != 0)
+        {
+            current = current->next;
+        }
+        current->next = new_node;
+    }
+
+    return current->next;
+}
+
+template<class retT>
+retT *LinkedList2<retT>::remove(uint32_t loc)
+{
+    retT *current = _head;
+    retT *prev = 0;
+    if ((loc <= length()) && (loc > 0))
+    {
+        if (1 == loc)
+        {
+            _head = current->next;
+            delete [] current;
+        }
+        else
+        {
+            for (uint32_t i=2; i<=loc; ++i)
+            {
+                prev = current;
+                current = current->next;
+            }
+            prev->next = current->next;
+            delete [] current;
+        }
+    }
+
+    return _head;
+}
+
+template<class retT>
+retT *LinkedList2<retT>::pop(uint32_t loc)
+{
+    retT *current = _head;
+    if ((loc > length()) || (loc == 0))
+    {
+        return 0;
+    }
+    for (uint32_t i=2; i<=loc; ++i)
+    {
+        current = current->next;
+    }
+    return current;
+}
+
+template<class retT>
+uint32_t LinkedList2<retT>::length(void)
+{
+    int32_t count = 0;
+    retT *current = _head;
+    while (current != 0)
+    {
+        ++count;
+        current = current->next;
+    }
+    return count;
+}
+template class LinkedList2<node>;
diff -r 000000000000 -r 27649dfdde4c LinkedList2.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LinkedList2.h	Tue Dec 29 13:07:51 2020 +0000
@@ -0,0 +1,29 @@
+
+#ifndef LINKEDLIST_H_
+#define LINKEDLIST_H_
+
+#include <stdint.h>
+#include "mbed.h"
+
+struct node
+{
+    void *data;
+    struct node *next;
+};
+
+template<class retT>
+class LinkedList2
+{
+protected:
+    retT *_head;
+public:    
+    LinkedList2();    
+    ~LinkedList2();
+    retT *push(void *data);
+    retT *append(void *data);
+    retT *remove(uint32_t loc);
+    retT *pop(uint32_t loc);
+    uint32_t length(void);
+};
+
+#endif