From cf7626cb6450db1daf11e45daf1a91f2ccb84d22 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Mon, 21 Nov 2022 15:23:19 +0200 Subject: Add more helper functions on ll --- Seminar04/linked-list-utils.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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 @@ -21,6 +21,49 @@ void printLinkedList(const LinkedListNode* ll) { std::cout << std::endl; } +template +void printLinkedList(const LinkedListNode* ll, void (*print)(T)) { + const LinkedListNode* iterate = ll; + + while(iterate) { + print(iterate->data); + iterate = iterate->next; + } + + std::cout << std::endl; +} + +template +void printLinkedList(const LinkedListNode* ll, void (*print)(const T&)) { + const LinkedListNode* iterate = ll; + + while(iterate) { + print(iterate->data); + iterate = iterate->next; + } + + std::cout << std::endl; +} + +template +LinkedListNode* fromList(const std::initializer_list& l) { + LinkedListNode* toReturn = nullptr; + LinkedListNode* iter = nullptr; + + for(const auto& elem : l) { + LinkedListNode* toInsert = new LinkedListNode(elem); + if(iter == nullptr) { + toReturn = toInsert; + iter = toInsert; + } + else { + iter->next = toInsert; + iter = iter->next; + } + } + return toReturn; +} + template void freeList(LinkedListNode* ll) { while(ll) { -- cgit v1.3.1-23-gd82d