LinkedList icon indicating copy to clipboard operation
LinkedList copied to clipboard

Added iterator function to LinkedList

Open dindibo opened this issue 1 year ago • 0 comments

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.

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);
}

dindibo avatar Jun 21 '24 13:06 dindibo