Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar05 / src / singly-linked-list
diff options
context:
space:
mode:
Commitad40e20b8983831400502fa9617f4c842ae23b78 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2024-11-12 17:20:20 -0500
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2024-11-12 17:20:20 -0500
Filesa05c75eb0d2ed18672a5cd599f044173ac6371a0 /Seminar05/src/singly-linked-list
Data-structures-and-algorithms-ad40e20b8983831400502fa9617f4c842ae23b78.tar
Data-structures-and-algorithms-ad40e20b8983831400502fa9617f4c842ae23b78.tar.gz
Data-structures-and-algorithms-ad40e20b8983831400502fa9617f4c842ae23b78.zip
Parent450101630aeebfb92da99417a6a71570bdfeb3e6 (diff)
Add Seminar05
diff --git a/Seminar05/src/singly-linked-list/merge-sort.cpp b/Seminar05/src/singly-linked-list/merge-sort.cpp
new file mode 100644
index 0000000..aaeb6ef
--- /dev/null
+++ b/Seminar05/src/singly-linked-list/merge-sort.cpp
@@ -0,0 +1,81 @@
+#include "node_utils.h"
+#include <iostream>
+
+template <class T>
+node<T>* merge(node<T>* lhs, node<T>* rhs)
+{
+ // result linked list
+ node<T>* head = nullptr;
+ node<T>* tail = nullptr;
+
+ while (lhs && rhs)
+ {
+ if (lhs->data < rhs->data)
+ {
+ append_back(head, tail, lhs);
+ lhs = lhs->next;
+ }
+ else
+ {
+ append_back(head, tail, rhs);
+ rhs = rhs->next;
+ }
+ }
+
+ if (lhs)
+ {
+ tail->next = lhs;
+ }
+
+ if (rhs)
+ {
+ tail->next = rhs;
+ }
+
+ return head;
+}
+
+template <class T>
+node<T>* get_middle(node<T>* list)
+{
+ if (!list || !list->next)
+ return list;
+
+ node<T>* fast = list->next;
+ node<T>* slow = list;
+
+ while (fast && fast->next)
+ {
+ slow = slow->next;
+ fast = fast->next->next;
+ }
+ return slow;
+}
+
+template <class T>
+node<T>* merge_sort(node<T>* list)
+{
+ if (!list || !list->next)
+ return list;
+
+ node<T>* middle = get_middle(list);
+
+ node<T>* right_part = middle->next;
+ middle->next = nullptr;
+
+ node<T>* left_sorted = merge_sort(list);
+ node<T>* right_sorted = merge_sort(right_part);
+
+ return merge(left_sorted, right_sorted);
+}
+
+int main()
+{
+ node<int>* ll = new node(1, new node(0, new node(2, new node(-1, new node(10)))));
+ ll = merge_sort(ll);
+ if (is_sorted(ll) && list_size(ll) == 5)
+ std::cout << "passed";
+ else
+ std::cout << "failed";
+ free_list(ll);
+} \ No newline at end of file
diff --git a/Seminar05/src/singly-linked-list/node_utils.h b/Seminar05/src/singly-linked-list/node_utils.h
new file mode 100644
index 0000000..b439d4a
--- /dev/null
+++ b/Seminar05/src/singly-linked-list/node_utils.h
@@ -0,0 +1,79 @@
+#pragma once
+
+// We represent a singly linked list
+// with an pointer to its first element.
+template <class T>
+struct node
+{
+ T data;
+ node* next;
+
+ node(const T& data_, node* next_ = nullptr) :
+ data(data_),
+ next(next_) {}
+};
+
+template <class T>
+void append_back(node<T>*& head, node<T>*& tail, node<T>* element)
+{
+ if(head == nullptr)
+ {
+ head = tail = element;
+ }
+ else
+ {
+ tail->next = element;
+ tail = tail->next;
+ }
+}
+
+
+template <class T>
+bool is_sorted(node<T>* list)
+{
+ if(!list || !list->next)
+ return true;
+
+ node<T>* prev = list;
+ node<T>* next = list->next;
+
+ while(next)
+ {
+ if(prev->data > next->data)
+ {
+ return false;
+ }
+ prev = prev->next;
+ next = next->next;
+ }
+ return true;
+}
+
+template <class T>
+void free_list(node<T>* ll)
+{
+ while(ll)
+ {
+ node<T>* to_delete = ll;
+ ll = ll->next;
+ delete to_delete;
+ }
+}
+
+template <class T>
+unsigned list_size(node<T>* list)
+{
+ unsigned size = 0;
+ while(list)
+ {
+ ++size;
+ list = list->next;
+ }
+ return size;
+}
+
+template <class T>
+node<T>* push_front(node<T>* head, const T& data)
+{
+ return new node<T>(data, head);
+} \ No newline at end of file
diff --git a/Seminar05/src/singly-linked-list/quick_sort.cpp b/Seminar05/src/singly-linked-list/quick_sort.cpp
new file mode 100644
index 0000000..ed8044e
--- /dev/null
+++ b/Seminar05/src/singly-linked-list/quick_sort.cpp
@@ -0,0 +1,136 @@
+#include "node_utils.h"
+#include <cassert>
+#include <iostream>
+
+// Implementation is inspired by:
+// https://github.com/Angeld55/Data_structures_and_algorithms_FMI/blob/main/Miscellaneous/singlyLinkedList_quickSort.cpp
+
+template <class T>
+using list_info = std::pair<node<T>*, node<T>*>;
+
+template <class T, class Predicate>
+list_info<T> partition(node<T>* list, Predicate pred)
+{
+ node<T>* true_begin = nullptr;
+ node<T>* true_end = nullptr;
+
+ node<T>* false_begin = nullptr;
+ node<T>* false_end = nullptr;
+
+ while (list)
+ {
+ if (pred(list->data))
+ {
+ append_back(true_begin, true_end, list);
+ }
+ else
+ {
+ append_back(false_begin, false_end, list);
+ }
+ list = list->next;
+ }
+
+ if (true_end)
+ {
+ true_end->next = nullptr;
+ }
+ if (false_end)
+ {
+ false_end->next = nullptr;
+ }
+
+ return {true_begin, false_begin};
+}
+
+template <class T>
+list_info<T> concat_lists(list_info<T> left, list_info<T> right)
+{
+ if (left.first == nullptr)
+ return right;
+ if (right.first == nullptr)
+ return left;
+
+ left.second->next = right.first;
+ return {left.first, right.second};
+}
+
+template <class T>
+list_info<T> quick_sort_impl(node<T>* list)
+{
+ if (!list || !list->next)
+ return {list, list};
+
+ const T& pivot_element = list->data;
+ list_info<T> result = partition(list, [&pivot_element](const T& element) { return element < pivot_element; });
+
+ node<T>* pivot = result.second;
+
+ list_info<T> left_sorted = quick_sort_impl(result.first);
+
+ // Skip the pivot element.
+ list_info<T> right_sorted = quick_sort_impl(result.second->next);
+
+ // Attach pivot as the first element.
+ pivot->next = right_sorted.first;
+ right_sorted.first = pivot;
+
+ if (!right_sorted.second)
+ right_sorted.second = pivot;
+
+ return concat_lists(left_sorted, right_sorted);
+}
+
+template <class T>
+void quick_sort(node<T>*& list)
+{
+ list_info<T> result = quick_sort_impl(list);
+ list = result.first;
+}
+
+void tests()
+{
+ using inode = node<int>;
+
+ // test empty list
+ inode* ll = nullptr;
+ quick_sort(ll);
+ assert(ll == nullptr);
+
+ // One element case
+ ll = new inode(1);
+ quick_sort(ll);
+ assert(ll && ll->data == 1 && !ll->next);
+
+ // Two elements case (sorted & unsorted)
+ ll->next = new inode(0);
+ quick_sort(ll);
+ assert(ll && ll->data == 0 && ll->next && ll->next->data == 1 && !ll->next->next);
+ quick_sort(ll);
+ assert(ll && ll->data == 0 && ll->next && ll->next->data == 1 && !ll->next->next);
+
+ ll = push_front(ll, 2);
+ ll = push_front(ll, 3);
+
+ quick_sort(ll);
+ assert(is_sorted(ll) && list_size(ll) == 4);
+ quick_sort(ll);
+ assert(is_sorted(ll) && list_size(ll) == 4);
+
+ // Random elements
+ srand(time(0));
+
+ for (size_t i = 0; i < 1000; i++)
+ {
+ ll = push_front(ll, rand() % 10000);
+ }
+ assert(list_size(ll) == 1004);
+ quick_sort(ll);
+ assert(is_sorted(ll) && list_size(ll) == 1004);
+
+ free_list(ll);
+}
+
+int main()
+{
+ tests();
+} \ No newline at end of file
diff --git a/Seminar05/src/singly-linked-list/singly-linked-list-tests.cpp b/Seminar05/src/singly-linked-list/singly-linked-list-tests.cpp
new file mode 100644
index 0000000..89fe680
--- /dev/null
+++ b/Seminar05/src/singly-linked-list/singly-linked-list-tests.cpp
@@ -0,0 +1,98 @@
+#include "singly-linked-list.hpp"
+#include <iostream>
+#include <cassert>
+#include <forward_list>
+
+void push_tests()
+{
+ singly_linked_list<int> ll;
+ ll.push_back(1);
+
+ assert(ll.front() == 1 && ll.size() == 1);
+ ll.push_front(0);
+ assert(ll.front() == 0 && ll.back() == 1 && ll.size() == 2);
+
+ for (size_t i = 2; i < 100; i++)
+ ll.push_back(i);
+
+ assert(ll.size() == 100);
+
+ auto it = ll.begin();
+
+ for (size_t i = 0; i < 100; i++)
+ {
+ assert(*it == i);
+ ++it;
+ }
+
+ ll.remove_after(ll.begin());
+ assert(*(++ll.begin()) == 2 && ll.size() == 99);
+}
+
+void construct_tests()
+{
+ singly_linked_list<int> ll;
+ for (size_t i = 0; i < 100; i++)
+ {
+ ll.push_back(i);
+ }
+
+ singly_linked_list<int> copy(ll);
+
+ auto it = ll.begin();
+ auto it_copy = copy.begin();
+
+ while (true)
+ {
+ assert(*it == *it_copy);
+ ++it;
+ ++it_copy;
+
+ if(it == ll.end() || it_copy == copy.end())
+ break;
+ }
+
+ assert(ll.size() == copy.size());
+
+ singly_linked_list<int> move(std::move(ll));
+ ll = std::move(move);
+ move = std::move(ll);
+
+ auto its = move.begin();
+ for(const auto& x : copy)
+ {
+ assert(x == *its);
+ assert(its != move.end());
+ ++its;
+ }
+ assert(its == move.end());
+ assert(move.size() == 100);
+}
+
+void concat_tests()
+{
+ singly_linked_list<int> ll;
+ singly_linked_list<int> lls;
+
+ for (size_t i = 0; i < 50; i++)
+ {
+ ll.push_back(i);
+ lls.push_back(50 + i);
+ }
+
+ singly_linked_list<int> cat = concat(ll, lls);
+ assert(cat.size() == 100);
+ int val = 0;
+
+ for(const auto& x : cat)
+ {
+ assert(x == val++);
+ }
+}
+
+int main()
+{
+ push_tests();
+ construct_tests();
+ concat_tests();
+} \ No newline at end of file
diff --git a/Seminar05/src/singly-linked-list/singly-linked-list.hpp b/Seminar05/src/singly-linked-list/singly-linked-list.hpp
new file mode 100644
index 0000000..3ad895e
--- /dev/null
+++ b/Seminar05/src/singly-linked-list/singly-linked-list.hpp
@@ -0,0 +1,418 @@
+#pragma once
+#include <cstddef>
+#include <utility>
+
+template <typename T>
+class singly_linked_list
+{
+private:
+ struct node
+ {
+ T data;
+ node* next;
+
+ node(const T& data_, node* next_ = nullptr) :
+ data(data_),
+ next(next_) {}
+ };
+
+ node* _head = nullptr;
+ node* _tail = nullptr;
+ size_t _size = 0;
+public:
+ singly_linked_list() = default;
+ singly_linked_list(const singly_linked_list<T>& other);
+ singly_linked_list(singly_linked_list<T>&& other);
+ singly_linked_list<T>& operator=(const singly_linked_list<T>& other);
+ singly_linked_list<T>& operator=(singly_linked_list<T>&& other);
+ ~singly_linked_list();
+
+ void push_front(const T& el);
+ void push_back(const T& el);
+
+ void pop_front();
+
+ const T& front() const;
+ const T& back() const;
+
+ T& front();
+ T& back();
+
+ size_t size() const;
+ bool empty() const;
+
+ template <typename U>
+ friend singly_linked_list<U> concat(singly_linked_list<U>& lhs, singly_linked_list<U>& rhs);
+
+ class sll_iterator
+ {
+ node* currentElementPtr;
+
+ friend class singly_linked_list;
+ public:
+
+ sll_iterator(node* currentElementPtr = nullptr) : currentElementPtr(currentElementPtr) {}
+
+ T& operator*()
+ {
+ return currentElementPtr->data;
+ }
+
+ T* operator->()
+ {
+ return &currentElementPtr->data;
+ }
+
+ sll_iterator& operator++()
+ {
+ if (currentElementPtr)
+ currentElementPtr = currentElementPtr->next;
+ return *this;
+ }
+
+ sll_iterator operator++(int)
+ {
+ sll_iterator copy(*this);
+ ++(*this);
+ return copy;
+ }
+
+ sll_iterator& operator+=(size_t s)
+ {
+
+ while (s--)
+ ++(*this);
+
+ return *this;
+ }
+
+ sll_iterator operator+(int i) const
+ {
+
+ sll_iterator res(*this);
+ return res += i;
+ }
+
+ bool operator==(const sll_iterator& rhs) const { return currentElementPtr == rhs.currentElementPtr; }
+ bool operator!=(const sll_iterator& rhs) const { return !(rhs == *this); }
+
+ };
+
+ class const_sll_iterator
+ {
+ node* currentElementPtr;
+
+ friend class singly_linked_list;
+ public:
+ const_sll_iterator(const sll_iterator& nonConstIter) : currentElementPtr(nonConstIter.currentElementPtr) {}
+ const_sll_iterator(node* currentElementPtr = nullptr) : currentElementPtr(currentElementPtr) {}
+
+ const T& operator*() const
+ {
+ return currentElementPtr->data;
+ }
+
+ const T* operator->() const
+ {
+ return &currentElementPtr->data;
+ }
+
+ const_sll_iterator& operator++()
+ {
+ if (currentElementPtr)
+ currentElementPtr = currentElementPtr->next;
+ return *this;
+ }
+
+ const_sll_iterator operator++(int)
+ {
+ const_sll_iterator copy(*this);
+ ++(*this);
+ return copy;
+ }
+
+ const_sll_iterator& operator+=(size_t s)
+ {
+
+ while (s--)
+ ++(*this);
+
+ return *this;
+ }
+
+ const_sll_iterator operator+(int i) const
+ {
+
+ const_sll_iterator res(*this);
+ return res += i;
+ }
+
+ bool operator==(const const_sll_iterator& rhs) const { return currentElementPtr == rhs.currentElementPtr; }
+ bool operator!=(const const_sll_iterator& rhs) const { return !(rhs == *this); }
+
+ };
+
+ const_sll_iterator begin()
+ {
+ return sll_iterator(_head);
+ }
+ sll_iterator end()
+ {
+ return sll_iterator(nullptr);
+ }
+
+ const_sll_iterator cbegin() const
+ {
+ return const_sll_iterator(_head);
+ }
+ const_sll_iterator cend() const
+ {
+ return const_sll_iterator(nullptr);
+ }
+
+ sll_iterator insert_after(const T& element, const const_sll_iterator& it);
+ sll_iterator remove_after(const const_sll_iterator& it);
+
+private:
+ void copy(const singly_linked_list<T>& other);
+ void move(singly_linked_list<T>&& other);
+ void free();
+};
+
+template <typename T>
+void singly_linked_list<T>::push_front(const T& el)
+{
+ node* new_node = new node(el);
+
+ if (empty())
+ {
+ _head = _tail = new_node;
+ }
+ else
+ {
+ new_node->next = _head;
+ _head = new_node;
+ }
+ _size++;
+
+}
+template <typename T>
+void singly_linked_list<T>::push_back(const T& el)
+{
+ node* new_node = new node(el);
+
+ if (empty())
+ {
+ _head = _tail = new_node;
+ }
+ else
+ {
+ _tail->next = new_node;
+ _tail = new_node;
+ }
+ _size++;
+
+}
+
+template <typename T>
+void singly_linked_list<T>::pop_front()
+{
+ if (empty())
+ return;
+
+ if (_head == _tail)
+ {
+ delete _head;
+ _head = _tail = nullptr;
+ }
+ else
+ {
+ node* to_delete = _head;
+ _head = _head->next;
+ delete to_delete;
+ }
+ _size--;
+}
+
+template <typename T>
+const T& singly_linked_list<T>::front() const
+{
+ return _head->data;
+}
+
+template <typename T>
+const T& singly_linked_list<T>::back() const
+{
+ return _tail->data;
+}
+
+template <typename T>
+T& singly_linked_list<T>::front()
+{
+ return _head->data;
+}
+
+template <typename T>
+T& singly_linked_list<T>::back()
+{
+ return _tail->data;
+}
+
+template <typename T>
+singly_linked_list<T> concat(singly_linked_list<T>& lhs, singly_linked_list<T>& rhs)
+{
+ singly_linked_list<T> result;
+
+ if (!lhs._head)
+ {
+ result._head = rhs._head;
+ result._tail = rhs._tail;
+ }
+ else if (!rhs._head)
+ {
+ result._head = lhs._head;
+ result._tail = lhs._tail;
+ }
+ else
+ {
+ lhs._tail->next = rhs._head;
+ result._head = lhs._head;
+ result._tail = rhs._tail;
+ }
+ result._size = rhs._size + lhs._size;
+ rhs._size = lhs._size = 0;
+ rhs._head = rhs._tail = lhs._head = lhs._tail = nullptr;
+
+ return result;
+}
+
+template <typename T>
+typename singly_linked_list<T>::sll_iterator singly_linked_list<T>::insert_after(const T& element, const typename singly_linked_list<T>::const_sll_iterator& it)
+{
+ if(it == end())
+ return end();
+
+ node* new_node = new node(element);
+ node* itNode = it.currentElementPtr;
+
+ new_node->next = itNode->next;
+ itNode->next = new_node;
+ _size++;
+ return singly_linked_list<T>::sll_iterator(new_node);
+}
+
+
+template <typename T>
+typename singly_linked_list<T>::sll_iterator singly_linked_list<T>::remove_after(const typename singly_linked_list<T>::const_sll_iterator& it)
+{
+ if(it == end() || size() == 1)
+ return end();
+
+ node* to_delete = (it + 1).currentElementPtr;
+
+ node* new_next = to_delete->next;
+
+ it.currentElementPtr->next = new_next;
+ _size--;
+
+ if(to_delete == _tail)
+ {
+ _tail = it.currentElementPtr;
+ }
+
+ delete to_delete;
+
+ return singly_linked_list<T>::sll_iterator(new_next);
+}
+
+template <typename T>
+bool singly_linked_list<T>::empty() const
+{
+ return size() == 0;
+}
+
+template <typename T>
+size_t singly_linked_list<T>::size() const
+{
+ return _size;
+}
+
+template <typename T>
+singly_linked_list<T>::singly_linked_list(const singly_linked_list<T>& other)
+{
+ copy(other);
+}
+
+
+template <typename T>
+singly_linked_list<T>::singly_linked_list(singly_linked_list<T>&& other)
+{
+ move(std::move(other));
+}
+
+template <typename T>
+singly_linked_list<T>& singly_linked_list<T>::operator=(const singly_linked_list<T>& other)
+{
+ if (this != &other)
+ {
+ free();
+ copy(other);
+ }
+
+ return *this;
+}
+
+template <typename T>
+singly_linked_list<T>& singly_linked_list<T>::operator=(singly_linked_list<T>&& other)
+{
+ if (this != &other)
+ {
+ free();
+ move(std::move(other));
+ }
+
+ return *this;
+}
+template <typename T>
+singly_linked_list<T>::~singly_linked_list()
+{
+ free();
+}
+
+template <typename T>
+void singly_linked_list<T>::copy(const singly_linked_list<T>& other)
+{
+ node* iter = other._head;
+
+ while (iter)
+ {
+ push_back(iter->data);
+ iter = iter->next;
+ }
+}
+
+template <typename T>
+void singly_linked_list<T>::move(singly_linked_list<T>&& other)
+{
+ _head = other._head;
+ _tail = other._tail;
+ _size = other.size();
+
+ other._head = other._tail = nullptr;
+ other._size = 0;
+}
+
+template <typename T>
+void singly_linked_list<T>::free()
+{
+ node* iter = _head;
+
+ while (iter)
+ {
+ node* to_delete = iter;
+ iter = iter->next;
+ delete to_delete;
+ }
+ _head = _tail = nullptr;
+ _size = 0;
+} \ No newline at end of file