Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitcf7626cb6450db1daf11e45daf1a91f2ccb84d22 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-11-21 15:23:19 +0200
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-11-21 15:23:19 +0200
Files220c1e6c7620a71eed6434ef79579e31452dbc72
Data-structures-and-algorithms-cf7626cb6450db1daf11e45daf1a91f2ccb84d22.tar
Data-structures-and-algorithms-cf7626cb6450db1daf11e45daf1a91f2ccb84d22.tar.gz
Data-structures-and-algorithms-cf7626cb6450db1daf11e45daf1a91f2ccb84d22.zip
Parent7fbf4ea0b5679b83e637006b48ca1a287ebf3302 (diff)
Add more helper functions on ll
1 files changed, 43 insertions, 0 deletions
-rw-r--r--Seminar04/linked-list-utils.h43
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;