Skip to content

Commit 0307f1f

Browse files
authored
Merge pull request #2 from SloCompTech/Dev
Fixed some things and added easy access to items via array methods
2 parents 622cf86 + 4cf003a commit 0307f1f

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ Items on the list can accessed:
4949
int val3 = list.at(index);
5050
int val3 = list.get(index);
5151
```
52+
53+
Values of items that are already in the list, can be changed:
54+
``` C++
55+
/*
56+
WARNING !
57+
Note:
58+
- always check if index is in valid range before you try to change value of item
59+
*/
60+
list.at(index) = 3;
61+
list[index] = 3; // Same as above
62+
```
63+
5264
Size of list can be accessed with:
5365
``` C++
5466
int list_size = list.size();

examples/SimpleList/SimpleList.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ void setup()
1818
myList.push_back("First"); // Add item at the back of the line
1919
myList.push_back("Second");
2020
myList.push_front("New first"); // Ad item at the front of the line
21+
myList.at(1) = "NewSecond"; // Changed value of item in the list
22+
myList[0] = "NewNewFirst"; // Changed value of item in the list
2123
Serial.println("Items:");
2224
// Go through items
2325
for(int i=0;i<myList.size();i++)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=QList
2-
version=0.5
2+
version=0.5.1
33
author=SloCompTech
44
maintainer=SloCompTech <[email protected]>
55
sentence=Library implements linked lists

src/QList.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void QList<T>::clear()
135135
end = NULL;
136136
}
137137
template<class T>
138-
void QList<T>::clear(int index)
138+
void QList<T>::clear(unsigned int index)
139139
{
140140
node *tmp = start;
141141
for(int i=0;i<=index&&tmp!=NULL;i++)
@@ -160,7 +160,7 @@ void QList<T>::clear(int index)
160160

161161
// Get at index
162162
template<class T>
163-
T QList<T>::get(int index)
163+
T QList<T>::get(unsigned int index)
164164
{
165165
node *tmp = start;
166166
for(int i=0;i<=index&&tmp!=NULL;i++)
@@ -174,7 +174,7 @@ T QList<T>::get(int index)
174174
}
175175

176176
template<class T>
177-
T QList<T>::at(int index)
177+
T& QList<T>::at(unsigned int index)
178178
{
179179
node *tmp = start;
180180
for(int i=0;i<=index&&tmp!=NULL;i++)
@@ -184,7 +184,7 @@ T QList<T>::at(int index)
184184
else
185185
tmp=tmp->next;
186186
}
187-
return T(); // Return default value
187+
//TODO: Catch error when index is out of range
188188
}
189189

190190
// Get length
@@ -203,3 +203,33 @@ int QList<T>::indexOf(T val)
203203
return i;
204204
return -1;
205205
}
206+
207+
// Array operators
208+
template<class T>
209+
T& QList<T>::operator[](unsigned int index)
210+
{
211+
node *tmp = start;
212+
for(int i=0;i<=index&&tmp!=NULL;i++)
213+
{
214+
if(i==index)
215+
return tmp->item;
216+
else
217+
tmp=tmp->next;
218+
}
219+
//TODO: Catch error when index is out of range
220+
}
221+
222+
223+
template<class T>
224+
const T& QList<T>::operator[](unsigned int index) const
225+
{
226+
node *tmp = start;
227+
for(int i=0;i<=index&&tmp!=NULL;i++)
228+
{
229+
if(i==index)
230+
return tmp->item;
231+
else
232+
tmp=tmp->next;
233+
}
234+
//TODO: Catch error when index is out of range
235+
}

src/QList.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define NULL 0
1616
#endif
1717

18-
#include "Arduino.h"
18+
1919

2020
template<class T>
2121
class QList
@@ -42,9 +42,13 @@ class QList
4242
T back(); //!< get item from back >
4343
int size(); //!< Returns size of list >
4444
void clear(); //!< Clears list >
45-
void clear(int index); //!< Clears list >
46-
T get(int index); //!< Get item at given index >
47-
T at(int index); //!< Get item at given index >
45+
void clear(unsigned int index); //!< Clears list >
46+
T get(unsigned int index); //!< Get item at given index >
47+
T& at(unsigned int index); //!< Get item at given index >
48+
49+
// Array operator
50+
T& operator[](unsigned int index);
51+
const T& operator[](unsigned int index) const; // Not realy needed
4852

4953
// Non - critical functions
5054
int length();

0 commit comments

Comments
 (0)