diff options
Add more helper functions on ll
1 files changed, 43 insertions, 0 deletions
| -rw-r--r-- | Seminar04/linked-list-utils.h | 43 |
diff --git a/Seminar04/linked-list-utils.h b/Seminar04/linked-list-utils.h index 8e0fa23..bf093ba 100644 --- a/Seminar04/linked-list-utils.h +++ b/Seminar04/linked-list-utils.h @@ -22,6 +22,49 @@ void printLinkedList(const LinkedListNode<T>* ll) { } template<class T> +void printLinkedList(const LinkedListNode<T>* ll, void (*print)(T)) { + const LinkedListNode<T>* iterate = ll; + + while(iterate) { + print(iterate->data); + iterate = iterate->next; + } + + std::cout << std::endl; +} + +template<class T> +void printLinkedList(const LinkedListNode<T>* ll, void (*print)(const T&)) { + const LinkedListNode<T>* iterate = ll; + + while(iterate) { + print(iterate->data); + iterate = iterate->next; + } + + std::cout << std::endl; +} + +template<class T> +LinkedListNode<T>* fromList(const std::initializer_list<T>& l) { + LinkedListNode<T>* toReturn = nullptr; + LinkedListNode<T>* iter = nullptr; + + for(const auto& elem : l) { + LinkedListNode<T>* toInsert = new LinkedListNode<T>(elem); + if(iter == nullptr) { + toReturn = toInsert; + iter = toInsert; + } + else { + iter->next = toInsert; + iter = iter->next; + } + } + return toReturn; +} + +template<class T> void freeList(LinkedListNode<T>* ll) { while(ll) { LinkedListNode<T>* toDelete = ll; |
