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

Dependents:   DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LinkedList2.cpp Source File

LinkedList2.cpp

00001 
00002 #include "LinkedList2.h"
00003 
00004 template<class retT>
00005 LinkedList2<retT>::LinkedList2()
00006 {
00007     _head = 0;
00008     return;
00009 }
00010 
00011 template<class retT>
00012 LinkedList2<retT>::~LinkedList2()
00013 {
00014     while(remove(1) != NULL);
00015     return;
00016 }
00017 
00018 template<class retT>
00019 retT *LinkedList2<retT>::push(void *data)
00020 {
00021     retT *new_node = new retT [1];
00022     if (0 == new_node)
00023     {
00024         error("Memory allocation failed\n");
00025     }
00026     new_node->next = _head;
00027     new_node->data = data;
00028     _head = new_node;
00029     return _head;
00030 }
00031 
00032 template<class retT>
00033 retT *LinkedList2<retT>::append(void *data)
00034 {
00035     retT *current = _head;
00036     retT *new_node = new retT [1];
00037     if (0 == new_node)
00038     {
00039         error("Memory allocation failed\n");
00040     }
00041     new_node->data = data;
00042     new_node->next = 0;
00043     if (0 == current)
00044     {
00045         _head = new_node;
00046         return _head;
00047     }
00048     else
00049     {
00050         while (current->next != 0)
00051         {
00052             current = current->next;
00053         }
00054         current->next = new_node;
00055     }
00056 
00057     return current->next;
00058 }
00059 
00060 template<class retT>
00061 retT *LinkedList2<retT>::remove(uint32_t loc)
00062 {
00063     retT *current = _head;
00064     retT *prev = 0;
00065     if ((loc <= length()) && (loc > 0))
00066     {
00067         if (1 == loc)
00068         {
00069             _head = current->next;
00070             delete [] current;
00071         }
00072         else
00073         {
00074             for (uint32_t i=2; i<=loc; ++i)
00075             {
00076                 prev = current;
00077                 current = current->next;
00078             }
00079             prev->next = current->next;
00080             delete [] current;
00081         }
00082     }
00083 
00084     return _head;
00085 }
00086 
00087 template<class retT>
00088 retT *LinkedList2<retT>::pop(uint32_t loc)
00089 {
00090     retT *current = _head;
00091     if ((loc > length()) || (loc == 0))
00092     {
00093         return 0;
00094     }
00095     for (uint32_t i=2; i<=loc; ++i)
00096     {
00097         current = current->next;
00098     }
00099     return current;
00100 }
00101 
00102 template<class retT>
00103 uint32_t LinkedList2<retT>::length(void)
00104 {
00105     int32_t count = 0;
00106     retT *current = _head;
00107     while (current != 0)
00108     {
00109         ++count;
00110         current = current->next;
00111     }
00112     return count;
00113 }
00114 template class LinkedList2<node>;