From 4fc8093b565c94f94db807f96b2a279f118649e6 Mon Sep 17 00:00:00 2001 From: Niv Moshe Date: Fri, 21 Jun 2024 16:53:11 +0300 Subject: [PATCH] Added iterator function to LinkedList --- LinkedList.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/LinkedList.h b/LinkedList.h index 742933a..c89c3c9 100644 --- a/LinkedList.h +++ b/LinkedList.h @@ -96,6 +96,12 @@ class LinkedList{ */ virtual void clear(); + /* + Iterates over the LinkedList; + Calls the callback function for each element; + */ + virtual void iterator(void (*iteratorCallback)(T &, T *_out), T *out); + /* Sort the list, given a comparison function */ @@ -347,6 +353,22 @@ void LinkedList::clear(){ shift(); } + +template +void LinkedList::iterator(void (*iteratorCallback)(T &, T* _out), T *out){ + ListNode* current = root; + + while(current){ + + // Post data to callback function + iteratorCallback(current->data, out); + + current = current->next; + } + + return NULL; +} + template void LinkedList::sort(int (*cmp)(T &, T &)){ if(_size < 2) return; // trivial case;