From 5d3068ca66b2f55858cfd6336c72d15eac433c88 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 22 Nov 2022 13:41:16 +0200 Subject: [SDP/S04] Solved exercises from seminar 4 --- .../04-Seminar-26.10.2022/exercises.cpp | 145 +++++++++++++++++++++ .../04-Seminar-26.10.2022/linked-list-utils.h | 80 ++++++++++++ 2 files changed, 225 insertions(+) create mode 100644 Year 2/Structures and data algorithms/04-Seminar-26.10.2022/exercises.cpp create mode 100644 Year 2/Structures and data algorithms/04-Seminar-26.10.2022/linked-list-utils.h diff --git a/Year 2/Structures and data algorithms/04-Seminar-26.10.2022/exercises.cpp b/Year 2/Structures and data algorithms/04-Seminar-26.10.2022/exercises.cpp new file mode 100644 index 0000000..5fa8359 --- /dev/null +++ b/Year 2/Structures and data algorithms/04-Seminar-26.10.2022/exercises.cpp @@ -0,0 +1,145 @@ +#include "linked-list-utils.h" +#define LLN LinkedListNode +#include + +// Ex1 + +template +void insertBeg(LLN*& head, const T& elem) { + head = new LLN(elem, head); +} + +void ex1() { + LLN* head = new LLN(2, new LLN(3, new LLN(4))); + printLinkedList(head); + + insertBeg(head, 1); + printLinkedList(head); + + freeList(head); +} + +// Ex 2 + +template +void reverse(LLN*& head) { + LLN* newHead = nullptr; + LLN* iter; + while (head != nullptr) { + iter = head->next; + head->next = newHead; + newHead = head; + head = iter; + } + head = newHead; +} + +void ex2() { + LLN* head = new LLN(1, new LLN(2, new LLN(3, new LLN(4, new LLN(5))))); + printLinkedList(head); + + reverse(head); + printLinkedList(head); + + freeList(head); +} + +// Ex 3 + +template +LLN* inefficientMiddle(LLN* head) { + std::deque*> ptrs; + while (head != nullptr) { + ptrs.push_back(head); + head = head->next; + } + while (ptrs.size() > 2) { + ptrs.pop_front(); + ptrs.pop_back(); + } + return ptrs.front(); +} + +template +LLN* middle(LLN* head) { + LLN* fast = head, *slow = head; + while (fast && fast->next) { + fast = fast->next->next; + slow = slow->next; + } + return slow; +} + +void ex3() { + LLN* head1 = new LLN(1, new LLN(2, new LLN(3, new LLN(4, new LLN(5))))); + printLinkedList(head1); + LLN* head2 = new LLN(1, new LLN(2, new LLN(3, new LLN(4)))); + printLinkedList(head2); + + std::cout << middle(head1)->data << std::endl; + std::cout << middle(head2)->data << std::endl; + + freeList(head1); + freeList(head2); +} + +// Ex4 + +template +void reorder(LLN*& head) { + if (!head) return; + + LLN* iter = head, *plast = head, *tail = head; + while (iter->next != nullptr && iter->next->next != nullptr) { + plast = iter; + iter = iter->next; + while (plast->next->next != nullptr) + plast = plast->next; + tail->next = plast->next; + tail->next->next = iter; + tail = tail->next->next; + plast->next = nullptr; + } +} + +void ex4() { + LLN* head = new LLN(1, new LLN(2, new LLN(3, new LLN(4, new LLN(5))))); + printLinkedList(head); + + reorder(head); + printLinkedList(head); + + freeList(head); + + + head = new LLN(1, new LLN(2, new LLN(3, new LLN(4, new LLN(5, new LLN(6)))))); + printLinkedList(head); + + reorder(head); + printLinkedList(head); + + freeList(head); + + + head = new LLN(1); + printLinkedList(head); + + reorder(head); + printLinkedList(head); + + freeList(head); + + + head = nullptr; + printLinkedList(head); + + reorder(head); + printLinkedList(head); + + freeList(head); +} + + +int main() { + ex4(); +} diff --git a/Year 2/Structures and data algorithms/04-Seminar-26.10.2022/linked-list-utils.h b/Year 2/Structures and data algorithms/04-Seminar-26.10.2022/linked-list-utils.h new file mode 100644 index 0000000..013876c --- /dev/null +++ b/Year 2/Structures and data algorithms/04-Seminar-26.10.2022/linked-list-utils.h @@ -0,0 +1,80 @@ +/* Author : StoychoX + * Source : https://github.com/stoychoX/Data-structures-and-algorithms/blob/main/Seminar04/linked-list-utils.h + * License : + * Date : 22.11.2022 + */ + +#include +#include + +template +struct LinkedListNode { + T data; + LinkedListNode* next; + + LinkedListNode(const T& elem, LinkedListNode* n = nullptr) : data{elem}, next {n} {} +}; + +template +void printLinkedList(const LinkedListNode* ll) { + const LinkedListNode* iterate = ll; + + while(iterate) { + std::cout << iterate->data << " "; + iterate = iterate->next; + } + + 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) { + LinkedListNode* toDelete = ll; + ll = ll->next; + delete toDelete; + } +} -- cgit v1.3.1-23-gd82d