diff --git a/README.md b/README.md index 62e9f09..add5f03 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,18 @@ Items on the list can accessed: int val3 = list.at(index); int val3 = list.get(index); ``` + +Values of items that are already in the list, can be changed: +``` C++ + /* + WARNING ! + Note: + - always check if index is in valid range before you try to change value of item + */ + list.at(index) = 3; + list[index] = 3; // Same as above +``` + Size of list can be accessed with: ``` C++ int list_size = list.size(); diff --git a/examples/SimpleList/SimpleList.ino b/examples/SimpleList/SimpleList.ino index b387309..2bb478e 100644 --- a/examples/SimpleList/SimpleList.ino +++ b/examples/SimpleList/SimpleList.ino @@ -18,6 +18,8 @@ void setup() myList.push_back("First"); // Add item at the back of the line myList.push_back("Second"); myList.push_front("New first"); // Ad item at the front of the line + myList.at(1) = "NewSecond"; // Changed value of item in the list + myList[0] = "NewNewFirst"; // Changed value of item in the list Serial.println("Items:"); // Go through items for(int i=0;i sentence=Library implements linked lists diff --git a/src/QList.cpp b/src/QList.cpp index 60df552..6b28589 100644 --- a/src/QList.cpp +++ b/src/QList.cpp @@ -135,7 +135,7 @@ void QList::clear() end = NULL; } template -void QList::clear(int index) +void QList::clear(unsigned int index) { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) @@ -160,7 +160,7 @@ void QList::clear(int index) // Get at index template -T QList::get(int index) +T QList::get(unsigned int index) { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) @@ -174,7 +174,7 @@ T QList::get(int index) } template -T QList::at(int index) +T& QList::at(unsigned int index) { node *tmp = start; for(int i=0;i<=index&&tmp!=NULL;i++) @@ -184,7 +184,7 @@ T QList::at(int index) else tmp=tmp->next; } - return T(); // Return default value + //TODO: Catch error when index is out of range } // Get length @@ -203,3 +203,33 @@ int QList::indexOf(T val) return i; return -1; } + +// Array operators +template +T& QList::operator[](unsigned int index) +{ + node *tmp = start; + for(int i=0;i<=index&&tmp!=NULL;i++) + { + if(i==index) + return tmp->item; + else + tmp=tmp->next; + } + //TODO: Catch error when index is out of range +} + + +template +const T& QList::operator[](unsigned int index) const +{ + node *tmp = start; + for(int i=0;i<=index&&tmp!=NULL;i++) + { + if(i==index) + return tmp->item; + else + tmp=tmp->next; + } + //TODO: Catch error when index is out of range +} diff --git a/src/QList.h b/src/QList.h index 5dcb8a8..ef7bb2c 100644 --- a/src/QList.h +++ b/src/QList.h @@ -15,7 +15,7 @@ #define NULL 0 #endif -#include "Arduino.h" + template class QList @@ -42,9 +42,13 @@ class QList T back(); //!< get item from back > int size(); //!< Returns size of list > void clear(); //!< Clears list > - void clear(int index); //!< Clears list > - T get(int index); //!< Get item at given index > - T at(int index); //!< Get item at given index > + void clear(unsigned int index); //!< Clears list > + T get(unsigned int index); //!< Get item at given index > + T& at(unsigned int index); //!< Get item at given index > + + // Array operator + T& operator[](unsigned int index); + const T& operator[](unsigned int index) const; // Not realy needed // Non - critical functions int length();