From 7eee5a78005a9a569186d2c8b4b645a818ab9bf1 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Sun, 6 Oct 2024 07:04:44 -0400 Subject: Cleanup --- .../Additional-2023-SI/IntervalCollection.cpp | 103 ------------------ Additional/Additional-2023-SI/MergeIntoList.cpp | 117 --------------------- Additional/Additional-2023-SI/queue-insertion.cpp | 109 ------------------- Additional/Additional-2023-SI/readme.md | 13 --- Additional/Additional-2023-SI/rearrange.cpp | 112 -------------------- 5 files changed, 454 deletions(-) delete mode 100644 Additional/Additional-2023-SI/IntervalCollection.cpp delete mode 100644 Additional/Additional-2023-SI/MergeIntoList.cpp delete mode 100644 Additional/Additional-2023-SI/queue-insertion.cpp delete mode 100644 Additional/Additional-2023-SI/readme.md delete mode 100644 Additional/Additional-2023-SI/rearrange.cpp (limited to 'Additional/Additional-2023-SI') diff --git a/Additional/Additional-2023-SI/IntervalCollection.cpp b/Additional/Additional-2023-SI/IntervalCollection.cpp deleted file mode 100644 index 0ac2c76..0000000 --- a/Additional/Additional-2023-SI/IntervalCollection.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include -#include -#include - -class IntervalCollection -{ - using Iterator = std::vector>::iterator; - using ConstIterator = std::vector>::const_iterator; - - Iterator getRightmostInterval(int x) - { - std::pair searcher = { x, x }; - return std::lower_bound( - intervals.begin(), - intervals.end(), - searcher, - [](const std::pair& first, const std::pair& second) -> bool - { - return first.second < second.second; - } - ); - } - - ConstIterator getRightmostIntervalConst(int x) const - { - std::pair searcher = { x, x }; - return std::lower_bound( - intervals.begin(), - intervals.end(), - searcher, - [](const std::pair& first, const std::pair& second) -> bool - { - return first.second < second.second; - } - ); - } - -public: - void insert(int x) - { - Iterator right = getRightmostInterval(x); - bool begin = (right == intervals.begin()); - - Iterator left = begin ? intervals.end() : right - 1; - - bool needLeftMerge = !begin && (x - 1 == left->second); - bool needRightMerge = (right != intervals.end()) && (x + 1 == right->first); - - if (right != intervals.end() && x >= right->first && x <= right->second) - { - return; - } - - if (needLeftMerge && needRightMerge) - { - left->second = right->second; - intervals.erase(right); - } - else if (needLeftMerge) - { - left->second = x; - } - else if (needRightMerge) - { - right->first = x; - } - else - { - intervals.insert(right, { x, x }); - } - } - - bool contains(int x) const - { - ConstIterator right = getRightmostIntervalConst(x); - return (right != intervals.end() && x >= right->first && x <= right->second); - } - - void print() const - { - for (const auto& x : intervals) - { - std::cout << x.first << " " << x.second << std::endl; - } - } -private: - std::vector> intervals; -}; - -int main() -{ - IntervalCollection ic; - - ic.insert(1); - ic.insert(3); - ic.insert(2); - ic.insert(5); - ic.insert(7); - ic.insert(4); - ic.insert(6); - - std::cout << ic.contains(1) << " " << ic.contains(10); -} \ No newline at end of file diff --git a/Additional/Additional-2023-SI/MergeIntoList.cpp b/Additional/Additional-2023-SI/MergeIntoList.cpp deleted file mode 100644 index 7d575aa..0000000 --- a/Additional/Additional-2023-SI/MergeIntoList.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include - -struct ListNode -{ - int val; - ListNode *next; - ListNode(int x, ListNode *n = nullptr) : val(x), next(n) {} -}; - -ListNode *advance(ListNode *from, size_t index) -{ - while (index) - { - if (from == nullptr) - { - return nullptr; - } - - from = from->next; - --index; - } - - return from; -} - -ListNode *getTail(ListNode *list) -{ - if (!list || !list->next) - { - return list; - } - - while (list->next) - { - list = list->next; - } - return list; -} - -void freeRagne(ListNode *from, ListNode *to) -{ - while (from != to) - { - ListNode *node = from; - from = from->next; - delete node; - } -} - -void free(ListNode *ll) -{ - while (ll) - { - ListNode *toDelete = ll; - ll = ll->next; - delete toDelete; - } -} - -ListNode *addFront(ListNode *destination, ListNode *target, size_t to) -{ - ListNode *tail = getTail(target); - ListNode *end = advance(destination, to + 1); - tail->next = end; - freeRagne(destination, end); - return target; -} - -ListNode *mergeInBetween(ListNode *destination, ListNode *target, size_t from, size_t to) -{ - if (from == 0) - { - return addFront(destination, target, to); - } - - ListNode *predBegin = advance(destination, from - 1); - if (!predBegin) - { - return nullptr; - } - - ListNode *postEnd = advance(predBegin, to - from + 2); - ListNode *toDeleteHolder = predBegin->next; - ListNode *targetTail = getTail(target); - - predBegin->next = target; - targetTail->next = postEnd; - - freeRagne(toDeleteHolder, postEnd); - - return destination; -} - -int main() -{ - ListNode *fst = new ListNode(1, - new ListNode(2, - new ListNode(3, - new ListNode(4, - new ListNode(6))))); - - ListNode *snd = new ListNode(3, - new ListNode(4, - new ListNode(5))); - - ListNode *ans = mergeInBetween(fst, snd, 1, 3); - - ListNode *it = ans; - - while(it) - { - std::cout << it->val << " "; - it = it->next; - } - - free(ans); -} \ No newline at end of file diff --git a/Additional/Additional-2023-SI/queue-insertion.cpp b/Additional/Additional-2023-SI/queue-insertion.cpp deleted file mode 100644 index f27416a..0000000 --- a/Additional/Additional-2023-SI/queue-insertion.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include - -struct Node -{ - Node* next = nullptr; - int data = 0; - Node(int data, Node* next = nullptr) : data(data), next(next){} -}; - -struct Queue -{ - Node* head = nullptr; - Node* tail = nullptr; -}; - -void push(Queue& q, int el) -{ - if (q.head == nullptr) - q.head = q.tail = new Node(el); - else - { - Node* newNode = new Node(el); - q.tail->next = newNode; - - q.tail = newNode; - } -} -void pop(Queue& q) -{ - if (!q.head) - return; - Node* toDelete = q.head; - q.head = q.head->next; - delete toDelete; -} - -void insert(Queue& q, int el) -{ - Node* iter = q.head; - - while (iter) - { - if (iter->data % 10 == el % 10) - { - break; - } - iter = iter->next; - } - - if (iter) - { - Node* newNode = new Node(el); - newNode->next = iter->next; - iter->next = newNode; - } - else - { - if (!q.head) - { - Node* newNode = new Node(el); - q.head = q.tail = newNode; - } - else - { - Node* newNode = new Node(el); - q.tail->next = newNode; - q.tail = newNode; - } - } -} - -void print(const Queue& q) -{ - const Node* it = q.head; - - while(it) - { - std::cout << it->data << " "; - - it = it->next; - } -} -void free(Queue& q) -{ - while (q.head) - { - Node* toDelete = q.head; - q.head = q.head->next; - - delete toDelete; - } - -} -int main() -{ - Queue q; - - push(q, 1); - push(q, 2); - push(q, 4); - push(q, 8); - push(q, 16); - - insert(q, 12); - - print(q); - - free(q); -} \ No newline at end of file diff --git a/Additional/Additional-2023-SI/readme.md b/Additional/Additional-2023-SI/readme.md deleted file mode 100644 index 8ffe953..0000000 --- a/Additional/Additional-2023-SI/readme.md +++ /dev/null @@ -1,13 +0,0 @@ -## Разгледани структури от STL - -* std::vector -* std::list -* std::forward_list - -## Разгледани функции от STL -* std::sort() -* std::lower_bound() -* std::upper_bound() -* std::find() -* std::find_if() -* std::list::splice \ No newline at end of file diff --git a/Additional/Additional-2023-SI/rearrange.cpp b/Additional/Additional-2023-SI/rearrange.cpp deleted file mode 100644 index e5ad080..0000000 --- a/Additional/Additional-2023-SI/rearrange.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#include - -struct Node -{ - Node* next = nullptr; - Node* prev = nullptr; - int data = 0; - Node(int data, Node* next = nullptr, Node* prev = nullptr) : data(data), next(next), prev(prev) {} -}; - -Node* rearange(Node* head) -{ - Node* evensBegin = nullptr; - Node* evensEnd = nullptr; - Node* oddsBegin = nullptr; - Node* oddsEnd = nullptr; - - Node* iter = head; - - while (iter) - { - if (iter->data % 2 == 0) - { - if (!evensBegin) - { - evensBegin = evensEnd = iter; - iter->prev = nullptr; - } - else - { - evensEnd->next = iter; - iter->prev = evensEnd; - evensEnd = iter; - } - } - else - { - if (!oddsBegin) - { - oddsBegin = oddsEnd = iter; - iter->prev = nullptr; - } - else - { - oddsEnd->next = iter; - iter->prev = oddsEnd; - oddsEnd = iter; - } - } - iter = iter->next; - } - if(evensEnd) - evensEnd->next = nullptr; - if(oddsEnd) - oddsEnd->next = nullptr; - - // concat - if (!evensBegin) - return oddsBegin; - if (!oddsBegin) - return evensBegin; - - evensEnd->next = oddsBegin; - oddsBegin->prev = evensEnd; - return evensBegin; -} - -Node* push_front(Node* list, int elem) -{ - if(!list) - { - return new Node(elem); - } - - Node* toReturn = new Node(elem, list); - list->prev = toReturn; - return toReturn; -} - -void free(Node* l) -{ - Node* it = l; - - while(it) - { - Node* toDelete = it; - it = it->next; - delete toDelete; - } -} - -int main() -{ - Node* l = nullptr; - - for (size_t i = 0; i < 50; i++) - { - l = push_front(l, 50 - i); - } - - l = rearange(l); - - Node* it = l; - - while(it) - { - std::cout << it->data << " "; - it = it->next; - } - - free(l); -} \ No newline at end of file -- cgit v1.3.1-26-gb76a