Skip to content

Conversation

@dindibo
Copy link

@dindibo dindibo commented Jun 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant