LinkedList
LinkedList copied to clipboard
Added iterator function to LinkedList
Description
Feature Addition: Iterator Function for LinkedList
This commit adds a new iterator function to the LinkedList class. The function allows for iterating over the linked list and executing a callback function on each element.
Changes
-
New Function:
iterator(void (*iteratorCallback)(T &, T *_out), T *out)- Iterates over the
LinkedList. - Calls the provided callback function for each element.
- The callback function takes a reference to the current element and a pointer to an output variable.
- Iterates over the
Usage
This new iterator function can be used to perform operations on each element of the LinkedList by providing a callback function that defines the desired behavior.
Example
LinkedList<int> *movingAvg;
void iteratorCallback__avg(int &val, int *out){
*out = *out + val;
}
int average(LinkedList<int> *arr){
int avg = 0;
arr->iterator(iteratorCallback__avg, &avg);
return avg / arr->size();
}
void setup() {
Serial.begin(9600);
lst = new LinkedList<int>();
lst->add(10);
lst->add(20);
lst->add(30);
int avg = average(lst);
}