From ad40e20b8983831400502fa9617f4c842ae23b78 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Tue, 12 Nov 2024 17:20:20 -0500 Subject: Add Seminar05 --- Seminar05/src/singly-linked-list/merge-sort.cpp | 81 ++++ Seminar05/src/singly-linked-list/node_utils.h | 79 ++++ Seminar05/src/singly-linked-list/quick_sort.cpp | 136 +++++++ .../singly-linked-list-tests.cpp | 98 +++++ .../src/singly-linked-list/singly-linked-list.hpp | 418 +++++++++++++++++++++ 5 files changed, 812 insertions(+) create mode 100644 Seminar05/src/singly-linked-list/merge-sort.cpp create mode 100644 Seminar05/src/singly-linked-list/node_utils.h create mode 100644 Seminar05/src/singly-linked-list/quick_sort.cpp create mode 100644 Seminar05/src/singly-linked-list/singly-linked-list-tests.cpp create mode 100644 Seminar05/src/singly-linked-list/singly-linked-list.hpp (limited to 'Seminar05/src/singly-linked-list') 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 + +template +node* merge(node* lhs, node* rhs) +{ + // result linked list + node* head = nullptr; + node* 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 +node* get_middle(node* list) +{ + if (!list || !list->next) + return list; + + node* fast = list->next; + node* slow = list; + + while (fast && fast->next) + { + slow = slow->next; + fast = fast->next->next; + } + return slow; +} + +template +node* merge_sort(node* list) +{ + if (!list || !list->next) + return list; + + node* middle = get_middle(list); + + node* right_part = middle->next; + middle->next = nullptr; + + node* left_sorted = merge_sort(list); + node* right_sorted = merge_sort(right_part); + + return merge(left_sorted, right_sorted); +} + +int main() +{ + node* 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 +struct node +{ + T data; + node* next; + + node(const T& data_, node* next_ = nullptr) : + data(data_), + next(next_) {} +}; + +template +void append_back(node*& head, node*& tail, node* element) +{ + if(head == nullptr) + { + head = tail = element; + } + else + { + tail->next = element; + tail = tail->next; + } +} + + +template +bool is_sorted(node* list) +{ + if(!list || !list->next) + return true; + + node* prev = list; + node* next = list->next; + + while(next) + { + if(prev->data > next->data) + { + return false; + } + prev = prev->next; + next = next->next; + } + return true; +} + +template +void free_list(node* ll) +{ + while(ll) + { + node* to_delete = ll; + ll = ll->next; + delete to_delete; + } +} + +template +unsigned list_size(node* list) +{ + unsigned size = 0; + while(list) + { + ++size; + list = list->next; + } + return size; +} + +template +node* push_front(node* head, const T& data) +{ + return new node(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 +#include + +// Implementation is inspired by: +// https://github.com/Angeld55/Data_structures_and_algorithms_FMI/blob/main/Miscellaneous/singlyLinkedList_quickSort.cpp + +template +using list_info = std::pair*, node*>; + +template +list_info partition(node* list, Predicate pred) +{ + node* true_begin = nullptr; + node* true_end = nullptr; + + node* false_begin = nullptr; + node* 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 +list_info concat_lists(list_info left, list_info right) +{ + if (left.first == nullptr) + return right; + if (right.first == nullptr) + return left; + + left.second->next = right.first; + return {left.first, right.second}; +} + +template +list_info quick_sort_impl(node* list) +{ + if (!list || !list->next) + return {list, list}; + + const T& pivot_element = list->data; + list_info result = partition(list, [&pivot_element](const T& element) { return element < pivot_element; }); + + node* pivot = result.second; + + list_info left_sorted = quick_sort_impl(result.first); + + // Skip the pivot element. + list_info 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 +void quick_sort(node*& list) +{ + list_info result = quick_sort_impl(list); + list = result.first; +} + +void tests() +{ + using inode = node; + + // 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 +#include +#include + +void push_tests() +{ + singly_linked_list 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 ll; + for (size_t i = 0; i < 100; i++) + { + ll.push_back(i); + } + + singly_linked_list 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 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 ll; + singly_linked_list lls; + + for (size_t i = 0; i < 50; i++) + { + ll.push_back(i); + lls.push_back(50 + i); + } + + singly_linked_list 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 +#include + +template +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& other); + singly_linked_list(singly_linked_list&& other); + singly_linked_list& operator=(const singly_linked_list& other); + singly_linked_list& operator=(singly_linked_list&& 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 + friend singly_linked_list concat(singly_linked_list& lhs, singly_linked_list& 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 ¤tElementPtr->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 ¤tElementPtr->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& other); + void move(singly_linked_list&& other); + void free(); +}; + +template +void singly_linked_list::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 +void singly_linked_list::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 +void singly_linked_list::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 +const T& singly_linked_list::front() const +{ + return _head->data; +} + +template +const T& singly_linked_list::back() const +{ + return _tail->data; +} + +template +T& singly_linked_list::front() +{ + return _head->data; +} + +template +T& singly_linked_list::back() +{ + return _tail->data; +} + +template +singly_linked_list concat(singly_linked_list& lhs, singly_linked_list& rhs) +{ + singly_linked_list 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 singly_linked_list::sll_iterator singly_linked_list::insert_after(const T& element, const typename singly_linked_list::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::sll_iterator(new_node); +} + + +template +typename singly_linked_list::sll_iterator singly_linked_list::remove_after(const typename singly_linked_list::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::sll_iterator(new_next); +} + +template +bool singly_linked_list::empty() const +{ + return size() == 0; +} + +template +size_t singly_linked_list::size() const +{ + return _size; +} + +template +singly_linked_list::singly_linked_list(const singly_linked_list& other) +{ + copy(other); +} + + +template +singly_linked_list::singly_linked_list(singly_linked_list&& other) +{ + move(std::move(other)); +} + +template +singly_linked_list& singly_linked_list::operator=(const singly_linked_list& other) +{ + if (this != &other) + { + free(); + copy(other); + } + + return *this; +} + +template +singly_linked_list& singly_linked_list::operator=(singly_linked_list&& other) +{ + if (this != &other) + { + free(); + move(std::move(other)); + } + + return *this; +} +template +singly_linked_list::~singly_linked_list() +{ + free(); +} + +template +void singly_linked_list::copy(const singly_linked_list& other) +{ + node* iter = other._head; + + while (iter) + { + push_back(iter->data); + iter = iter->next; + } +} + +template +void singly_linked_list::move(singly_linked_list&& other) +{ + _head = other._head; + _tail = other._tail; + _size = other.size(); + + other._head = other._tail = nullptr; + other._size = 0; +} + +template +void singly_linked_list::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 -- cgit v1.3.1-26-gb76a