Skip to content

Commit

Permalink
Merge pull request #2 from SloCompTech/Dev
Browse files Browse the repository at this point in the history
Fixed some things and added easy access to items via array methods
  • Loading branch information
SloCompTech authored Feb 17, 2017
2 parents 622cf86 + 4cf003a commit 0307f1f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions examples/SimpleList/SimpleList.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<myList.size();i++)
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=QList
version=0.5
version=0.5.1
author=SloCompTech
maintainer=SloCompTech <[email protected]>
sentence=Library implements linked lists
Expand Down
38 changes: 34 additions & 4 deletions src/QList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void QList<T>::clear()
end = NULL;
}
template<class T>
void QList<T>::clear(int index)
void QList<T>::clear(unsigned int index)
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
Expand All @@ -160,7 +160,7 @@ void QList<T>::clear(int index)

// Get at index
template<class T>
T QList<T>::get(int index)
T QList<T>::get(unsigned int index)
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
Expand All @@ -174,7 +174,7 @@ T QList<T>::get(int index)
}

template<class T>
T QList<T>::at(int index)
T& QList<T>::at(unsigned int index)
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
Expand All @@ -184,7 +184,7 @@ T QList<T>::at(int index)
else
tmp=tmp->next;
}
return T(); // Return default value
//TODO: Catch error when index is out of range
}

// Get length
Expand All @@ -203,3 +203,33 @@ int QList<T>::indexOf(T val)
return i;
return -1;
}

// Array operators
template<class T>
T& QList<T>::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<class T>
const T& QList<T>::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
}
12 changes: 8 additions & 4 deletions src/QList.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define NULL 0
#endif

#include "Arduino.h"


template<class T>
class QList
Expand All @@ -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();
Expand Down

0 comments on commit 0307f1f

Please sign in to comment.