Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / 04-Seminar-26.10.2022 / exercises.cpp
diff options
context:
space:
mode:
Commit5d3068ca66b2f55858cfd6336c72d15eac433c88 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-22 13:41:16 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-22 13:41:16 +0200
Files3013aad020f017ac52e3889d4d09112076d5c169 /Year 2/Structures and data algorithms/04-Seminar-26.10.2022/exercises.cpp
uni-projects-5d3068ca66b2f55858cfd6336c72d15eac433c88.tar
uni-projects-5d3068ca66b2f55858cfd6336c72d15eac433c88.tar.gz
uni-projects-5d3068ca66b2f55858cfd6336c72d15eac433c88.zip
Parent2766c68912a2d463de1f5714569c2457c34d6215 (diff)
[SDP/S04] Solved exercises from seminar 4
1 files changed, 145 insertions, 0 deletions
-rw-r--r--Year 2/Structures and data algorithms/04-Seminar-26.10.2022/exercises.cpp145
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 <deque>
+
+// Ex1
+
+template <class T>
+void insertBeg(LLN<T>*& head, const T& elem) {
+ head = new LLN<int>(elem, head);
+}
+
+void ex1() {
+ LLN<int>* head = new LLN<int>(2, new LLN<int>(3, new LLN<int>(4)));
+ printLinkedList(head);
+
+ insertBeg(head, 1);
+ printLinkedList(head);
+
+ freeList(head);
+}
+
+// Ex 2
+
+template <class T>
+void reverse(LLN<T>*& head) {
+ LLN<T>* newHead = nullptr;
+ LLN<T>* iter;
+ while (head != nullptr) {
+ iter = head->next;
+ head->next = newHead;
+ newHead = head;
+ head = iter;
+ }
+ head = newHead;
+}
+
+void ex2() {
+ LLN<int>* head = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5)))));
+ printLinkedList(head);
+
+ reverse(head);
+ printLinkedList(head);
+
+ freeList(head);
+}
+
+// Ex 3
+
+template <class T>
+LLN<T>* inefficientMiddle(LLN<T>* head) {
+ std::deque<LLN<T>*> 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 <class T>
+LLN<T>* middle(LLN<T>* head) {
+ LLN<T>* fast = head, *slow = head;
+ while (fast && fast->next) {
+ fast = fast->next->next;
+ slow = slow->next;
+ }
+ return slow;
+}
+
+void ex3() {
+ LLN<int>* head1 = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5)))));
+ printLinkedList(head1);
+ LLN<int>* head2 = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4))));
+ printLinkedList(head2);
+
+ std::cout << middle(head1)->data << std::endl;
+ std::cout << middle(head2)->data << std::endl;
+
+ freeList(head1);
+ freeList(head2);
+}
+
+// Ex4
+
+template <class T>
+void reorder(LLN<T>*& head) {
+ if (!head) return;
+
+ LLN<T>* 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<int>* head = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5)))));
+ printLinkedList(head);
+
+ reorder(head);
+ printLinkedList(head);
+
+ freeList(head);
+
+
+ head = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5, new LLN<int>(6))))));
+ printLinkedList(head);
+
+ reorder(head);
+ printLinkedList(head);
+
+ freeList(head);
+
+
+ head = new LLN<int>(1);
+ printLinkedList(head);
+
+ reorder(head);
+ printLinkedList(head);
+
+ freeList(head);
+
+
+ head = nullptr;
+ printLinkedList(head);
+
+ reorder(head);
+ printLinkedList(head);
+
+ freeList(head);
+}
+
+
+int main() {
+ ex4();
+}