From 46b1d3a0486960302797315be1f4ad63c5b5d32c Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Wed, 5 Nov 2025 11:34:55 +0300 Subject: [PATCH 1/6] add DSU_impl + tests --- CMakeLists.txt | 5 +- lib_DSU/CMakeLists.txt | 1 + lib_DSU/DSU.cpp | 0 lib_DSU/DSU.h | 57 +++++++ lib_list/CMakeLists.txt | 1 + lib_list/List.cpp | 0 lib_list/List.h | 279 +++++++++++++++++++++++++++++++++ lib_queue/CMakeLists.txt | 1 + lib_queue/queue.cpp | 0 lib_queue/queue.h | 173 ++++++++++++++++++++ lib_stack/CMakeLists.txt | 1 + lib_stack/Stack.cpp | 0 lib_stack/Stack.h | 75 +++++++++ tests/test_DSU.cpp | 56 +++++++ tests/test_easy_example.cpp | 304 ++++++++++++++++++++++++++++++++---- 15 files changed, 921 insertions(+), 32 deletions(-) create mode 100644 lib_DSU/CMakeLists.txt create mode 100644 lib_DSU/DSU.cpp create mode 100644 lib_DSU/DSU.h create mode 100644 lib_list/CMakeLists.txt create mode 100644 lib_list/List.cpp create mode 100644 lib_list/List.h create mode 100644 lib_queue/CMakeLists.txt create mode 100644 lib_queue/queue.cpp create mode 100644 lib_queue/queue.h create mode 100644 lib_stack/CMakeLists.txt create mode 100644 lib_stack/Stack.cpp create mode 100644 lib_stack/Stack.h create mode 100644 tests/test_DSU.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fb5abf3a..c6596642 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,10 @@ include(cmake/function.cmake) # подхватываем функции, # и для создания исполняемого проекта в отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example - +add_subdirectory(lib_DSU) +add_subdirectory(lib_list) +add_subdirectory(lib_stack) +add_subdirectory(lib_queue) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_DSU/CMakeLists.txt b/lib_DSU/CMakeLists.txt new file mode 100644 index 00000000..424d2417 --- /dev/null +++ b/lib_DSU/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(DSU) \ No newline at end of file diff --git a/lib_DSU/DSU.cpp b/lib_DSU/DSU.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_DSU/DSU.h b/lib_DSU/DSU.h new file mode 100644 index 00000000..6b6efbcd --- /dev/null +++ b/lib_DSU/DSU.h @@ -0,0 +1,57 @@ +class DSU { + + int* _parent; + size_t _size; + int* _rank; + +public: + + DSU(size_t size); + void unite(int, int); + int find(int); + ~DSU(); + +}; + +DSU::DSU(size_t size) : _size(size) { + + _parent = new int[size]; + _rank = new int[size]; + for (int i = 0; i < _size; i++) { + _parent[i] = i; + } + + +} + +DSU::~DSU() { + + delete[] _parent; + +} + +int DSU::find(int x) { + + if (_parent[x] == x) { + return x; + } + else { + return find(_parent[x]); + } + +} + + +void DSU::unite(int x1, int x2) { + + if (_rank[x1] < _rank[x2]) { + _parent[x1] = find(x2); + } + else { + if (_rank[x1] == _rank[x2]) { + _rank[x1]++; + } + _parent[x2] = find(x1); + } + +} \ No newline at end of file diff --git a/lib_list/CMakeLists.txt b/lib_list/CMakeLists.txt new file mode 100644 index 00000000..148b539a --- /dev/null +++ b/lib_list/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(List) \ No newline at end of file diff --git a/lib_list/List.cpp b/lib_list/List.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_list/List.h b/lib_list/List.h new file mode 100644 index 00000000..a8a64869 --- /dev/null +++ b/lib_list/List.h @@ -0,0 +1,279 @@ +#pragma once + + +template +struct Node { + + T value; + Node* next; + Node(T value_, Node* next_ = nullptr); +}; + +template +Node::Node(T value_, Node* next_) : value(value_), next(next_) {} + +template +class List { + + Node* _head; + Node* _tail; + int _count; + +public: + + + class Iterator { + + + Node* current; + + public: + + Iterator() : current(nullptr) {} + Iterator(Node* node) : current(node) {} + + Iterator& operator=(const Iterator& other) { + current = other.current; + return *this; + } + + T& operator*() { + return current->value; + } + + Iterator& operator++() { + if (current != nullptr) { + current = current->next; + } + return *this; + } + + Iterator operator++(int) { + Iterator temp = *this; + ++(*this); + return temp; + } + + bool operator==(const Iterator& other) const { + return current == other.current; + } + + bool operator!=(const Iterator& other) const { + return current != other.current; + } + + }; + + + + List() : _head(nullptr), _tail(nullptr), _count(0) {} + List(const List& other) { + _head = nullptr; + _tail = nullptr; + _count = 0; + + Node* current = other._head; + while (current != nullptr) { + push_back(current->value); + current = current->next; + } + } + + ~List() { + while (!is_empty()) { + pop_front(); + } + } + + List& operator=(const List& other) { + if (this != &other) { + while (!is_empty()) { + pop_front(); + } + + Node* current = other._head; + while (current != nullptr) { + push_back(current->value); + current = current->next; + } + } + return *this; + } + + void push_front(const T& val); + void push_back(const T& val); + void insert(int pos, const T& val); + void insert(Node* node, const T& val); + + void pop_front(); + void pop_back(); + T front() const; + T back() const; + void erase(Node* node); + + bool is_empty(); + Node* find(const T& val); + int size() const { return _count; } + +}; + +template +void List::push_front(const T& val) { + + Node* node = new Node(val, _head); + node->next = _head; + if (is_empty()) _tail = node; + _head = node; + _count++; + +} + +template +bool List::is_empty() { + return _head == nullptr; +} + +template +void List::push_back(const T& val) { + + Node* node = new Node(val); + // _tail->next = node; // -, + if (is_empty()) { + _head = node; + _tail = node; + _count++; + return; + } + + _tail->next = node; + _tail = node; + ++_count; + // nullptr + // (val) ---> Null + // node + +} + +template +void List::insert(Node* node, const T& val) { + if (node == nullptr) throw ("Error"); + Node* new_node = new Node(val, node->next); + node->next = new_node; + if (_tail == node) { + _tail = new_node; + } + _count++; +} + +template +void List::insert(int pos, const T& val) { + if (pos < 0 || pos >= _count) throw ("Error"); + + if (pos == 0) { + push_front(val); + } + else if (pos == _count - 1) { + push_back(val); + } + else { + int cur_pos = 0; + Node* cur = _head; + while (cur != nullptr) { + if (cur_pos == pos - 1) break; + cur_pos++; + cur = cur->next; + } + + if (cur != nullptr) { + Node* new_node = new Node(val, cur->next); + cur->next = new_node; + _count++; + } + } +} + +template +Node* List::find(const T& val) { + Node* current = _head; + while (current != nullptr) { + if (current->value == val) { + return current; + } + current = current->next; + } + return nullptr; +} + +template +void List::pop_front() { + if (is_empty()) throw ("List is empty"); + + Node* temp = _head; + _head = _head->next; + delete temp; + _count--; + + if (_head == nullptr) { + _tail = nullptr; + } +} + +template +void List::pop_back() { + if (is_empty()) throw ("List is empty"); + + if (_head == _tail) { + delete _head; + _head = nullptr; + _tail = nullptr; + _count = 0; + return; + } + + Node* current = _head; + while (current->next != _tail) { + current = current->next; + } + + delete _tail; + _tail = current; + _tail->next = nullptr; + _count--; +} + +template +T List::front() const { + if (is_empty()) throw ("List is empty"); + return _head->value; +} + +template +T List::back() const { + if (is_empty()) throw ("List is empty"); + return _tail->value; +} + + +template +void List::erase(Node* node) { + if (node == nullptr || is_empty()) return; + + if (node == _head) { + pop_front(); + return; + } + + Node* prev = _head; + while (prev != nullptr && prev->next != node) { + prev = prev->next; + } + + if (prev != nullptr) { + prev->next = node->next; + if (node == _tail) { + _tail = prev; + } + delete node; + _count--; + } +} diff --git a/lib_queue/CMakeLists.txt b/lib_queue/CMakeLists.txt new file mode 100644 index 00000000..d918c89d --- /dev/null +++ b/lib_queue/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Queue) diff --git a/lib_queue/queue.cpp b/lib_queue/queue.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_queue/queue.h b/lib_queue/queue.h new file mode 100644 index 00000000..9611de36 --- /dev/null +++ b/lib_queue/queue.h @@ -0,0 +1,173 @@ +template +class Queue { + T* _data; + int _head; + int _count; + int _capacity; + +public: + + Queue(int capacity = 15); + Queue(const Queue& other); + ~Queue(); + + Queue& operator=(const Queue& other); + Queue& operator=(Queue&& other) noexcept; + void enqueue(const T& value); + T dequeue(); + T& front(); + const T& front() const; + T& back(); + const T& back() const; + bool empty() const; + bool full() const; + int size() const; + int capacity() const; + void clear(); + int tail() const; + void resize(int newCapacity); + +}; + +template +Queue::Queue(int capacity) : _data(new T[capacity]), _head(0), _count(0), _capacity(capacity){} + +template +Queue::Queue(const Queue& other) : _data(new T[other._capacity]), _head(other._head), _count(other._count), _capacity(other._capacity) { + for (int i = 0; i < _count; i++) { + _data[(i + _head) % _capacity] = other._data[(i + other._head) % other._capacity]; + } +} + +template +Queue::~Queue() { + delete[] _data; +} + +template +Queue& Queue::operator=(const Queue& other) { + if (this != &other) { + delete[] _data; + _capacity = other._capacity; + _head = other._head; + _count = other._count; + _data = new T[_capacity]; + + for (int i = 0; i < _count; i++) { + _data[(i + _head) % _capacity] = other._data[(i + other._head) % other._capacity]; + } + } + return *this; +} + +template +Queue& Queue::operator=(Queue&& other) noexcept { + if (this != &other) { + delete[] _data; + _data = other._data; + _head = other._head; + _count = other._count; + _capacity = other._capacity; + + other._data = nullptr; + other._head = 0; + other._count = 0; + other._capacity = 0; + } + return *this; +} + +template +void Queue::enqueue(const T& value) { + if (full()) { + resize(_capacity * 2); + } + _data[tail()] = value; + _count++; +} + +template +T Queue::dequeue() { + if (empty()) { + throw std::runtime_error("Queue is empty"); + } + T value = std::move(_data[_head]); + _head = (_head + 1) % _capacity; + _count--; + return value; +} + +template +T& Queue::front() { + if (empty()) { + throw std::runtime_error("Queue is empty"); + } + return _data[_head]; +} + +template +const T& Queue::front() const { + if (empty()) { + throw std::runtime_error("Queue is empty"); + } + return _data[_head]; +} + +template +T& Queue::back() { + if (empty()) { + throw std::runtime_error("Queue is empty"); + } + return _data[tail() - 1]; +} + +template +const T& Queue::back() const { + if (empty()) { + throw std::runtime_error("Queue is empty"); + } + return _data[tail() - 1]; +} + +template +bool Queue::empty() const { + return _count == 0; +} + +template +bool Queue::full() const { + return _count == _capacity; +} + +template +int Queue::size() const { + return _count; +} + +template +int Queue::capacity() const { + return _capacity; +} + +template +void Queue::clear() { + _head = 0; + _count = 0; +} + +template +int Queue::tail() const { + return (_head + _count) % _capacity; +} + +template +void Queue::resize(int newCapacity) { + T* newData = new T[newCapacity]; + for (int i = 0; i < _count; i++) { + newData[i] = std::move(_data[(_head + i) % _capacity]); + } + delete[] _data; + _data = newData; + _head = 0; + _capacity = newCapacity; +} \ No newline at end of file diff --git a/lib_stack/CMakeLists.txt b/lib_stack/CMakeLists.txt new file mode 100644 index 00000000..b6bedb01 --- /dev/null +++ b/lib_stack/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Stack) \ No newline at end of file diff --git a/lib_stack/Stack.cpp b/lib_stack/Stack.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_stack/Stack.h b/lib_stack/Stack.h new file mode 100644 index 00000000..9921ee12 --- /dev/null +++ b/lib_stack/Stack.h @@ -0,0 +1,75 @@ +#pragma once +#include +template +class Stack { + +private: + + T* _data; + int _size, _top; + +public: + + Stack(int size); + Stack(); + void push(const T& val); + T pop(); + inline T top(); + inline bool is_empty() const noexcept; + inline bool is_full() const noexcept; + void clear(); + +}; + +template +inline bool Stack::is_empty() const noexcept { + + return _top == 0; + +} + +template +inline bool Stack::is_full() const noexcept { + + return _top == _size; + +} + +template +Stack::Stack(int size) : _data(new T[size]), _size(size), _top(0) {}; +template +Stack::Stack() : _data(nullptr), _size(0), _top(0) {}; + +template +void Stack::push(const T& val) { + + if (is_full()) { + throw std::logic_error("Queue is full"); + } + _data[++_top] = val; + +} + +template +T Stack::pop() { + + if (is_empty()) { + throw std::logic_error("Stack is empty"); + } + --_top; + +} +template +inline T Stack::top() { + if (is_empty()) { + throw std::logic_error("Stack is empty"); + } + return _data[_top]; +} + +template +void Stack::clear() { + + _top = 0; + +} \ No newline at end of file diff --git a/tests/test_DSU.cpp b/tests/test_DSU.cpp new file mode 100644 index 00000000..b1cf3086 --- /dev/null +++ b/tests/test_DSU.cpp @@ -0,0 +1,56 @@ +#include +#include "DSU.h" + + + + +TEST(DSUTest, BasicUnion) { + DSU dsu(10); + dsu.unite(0, 1); + EXPECT_EQ(dsu.find(0), dsu.find(1)); + + dsu.unite(2, 3); + EXPECT_EQ(dsu.find(2), dsu.find(3)); + + EXPECT_NE(dsu.find(0), dsu.find(2)); + EXPECT_NE(dsu.find(1), dsu.find(3)); +} + +TEST(DSUTest, TransitiveUnion) { + DSU dsu(10); + dsu.unite(0, 1); + dsu.unite(1, 2); + + EXPECT_EQ(dsu.find(0), dsu.find(1)); + EXPECT_EQ(dsu.find(1), dsu.find(2)); + EXPECT_EQ(dsu.find(0), dsu.find(2)); +} + +TEST(DSUTest, MultipleUnions) { + DSU dsu(10); + dsu.unite(0, 1); + dsu.unite(2, 3); + dsu.unite(0, 2); + + EXPECT_EQ(dsu.find(0), dsu.find(1)); + EXPECT_EQ(dsu.find(0), dsu.find(2)); + EXPECT_EQ(dsu.find(0), dsu.find(3)); +} + +TEST(DSUTest, SelfUnion) { + DSU dsu(10); + int original_root = dsu.find(5); + dsu.unite(5, 5); + EXPECT_EQ(dsu.find(5), original_root); +} + + +TEST(DSUTest, RankTest) { + DSU small_dsu(3); + + small_dsu.unite(0, 1); + small_dsu.unite(0, 2); + + EXPECT_EQ(small_dsu.find(0), small_dsu.find(1)); + EXPECT_EQ(small_dsu.find(0), small_dsu.find(2)); +} \ No newline at end of file diff --git a/tests/test_easy_example.cpp b/tests/test_easy_example.cpp index 3a67b612..fd9a8aee 100644 --- a/tests/test_easy_example.cpp +++ b/tests/test_easy_example.cpp @@ -2,49 +2,291 @@ #include #include "../lib_easy_example/easy_example.h" - #define EPSILON 0.000001 +#include "Queue.h" +#include "Stack.h" +#include "List.h" + + + +TEST(TestLibLib, can_iterate) { + + List list; + for (int i = 0; i < 10; i++) { + + list.push_back(i + 1); + + } + // Node* cur = list._head; + + + -TEST(TestEasyExampleLib, can_div) { - // Arrange - int x = 10; - int y = 2; +} + +TEST(TestQueue1, can_create_queue) { + Queue queue(10); + ASSERT_NO_THROW(queue); +} + +TEST(TestQueue2, can_enqueue) { + Queue queue(3); + queue.enqueue(17); + ASSERT_NO_THROW(queue); +} - // Act & Assert - ASSERT_NO_THROW(division(x, y)); +TEST(TestQueue3, can_dequeue) { + Queue queue(3); + queue.enqueue(17); + queue.enqueue(3); + queue.enqueue(2); + int result = queue.dequeue(); + ASSERT_NO_THROW(queue); + EXPECT_EQ(result, 17); } -TEST(TestEasyExampleLib, can_div_correctly) { - // Arrange - int x = 6; - int y = 2; +TEST(TestQueue4, can_take_front) { + Queue queue(3); + queue.enqueue(17); + queue.enqueue(3); + queue.enqueue(2); + EXPECT_EQ(queue.front(), 17); +} - // Act - int actual_result = division(x, y); +TEST(TestQueue6, check_is_empty) { + Queue queue(3); + EXPECT_EQ(queue.empty(), true); +} - // Assert - int expected_result = 3; - EXPECT_EQ(expected_result, actual_result); +TEST(TestQueue7, check_is_full) { + Queue queue(3); + queue.enqueue(17); + queue.enqueue(3); + queue.enqueue(2); + EXPECT_EQ(queue.full(), true); } -TEST(TestEasyExampleLib, can_div_correctly_with_remainder) { - // Arrange - int x = 5; - int y = 4; +TEST(TestQueue8, can_clear_queue) { + Queue queue(3); + queue.enqueue(17); + queue.enqueue(3); + queue.enqueue(2); + queue.clear(); + EXPECT_EQ(queue.empty(), true); +} - // Act - float actual_result = division(x, y); +TEST(TestQueue9, can_enqueue_after_clear) { + Queue queue(3); + queue.enqueue(17); + queue.enqueue(3); + queue.enqueue(2); + queue.clear(); + queue.enqueue(5); + EXPECT_EQ(queue.front(), 5); +} - // Assert - float expected_result = 1.25; - EXPECT_NEAR(expected_result, actual_result, EPSILON); +TEST(TestQueue10, can_get_size) { + Queue queue(3); + queue.enqueue(17); + queue.enqueue(3); + EXPECT_EQ(queue.size(), 2); } -TEST(TestEasyExampleLib, throw_when_try_div_by_zero) { - // Arrange - int x = 10; - int y = 0; +TEST(TestQueue11, can_get_capacity) { + Queue queue(7); + EXPECT_EQ(queue.capacity(), 7); +} - // Act & Assert - ASSERT_ANY_THROW(division(x, y)); +TEST(TestQueue12, fifo_order) { + Queue queue(3); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + EXPECT_EQ(queue.dequeue(), 1); + EXPECT_EQ(queue.dequeue(), 2); + EXPECT_EQ(queue.dequeue(), 3); } + + + +TEST(TestQueue16, auto_resize) { + Queue queue(2); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + EXPECT_EQ(queue.size(), 3); + EXPECT_EQ(queue.capacity(), 4); +} + +TEST(TestQueue17, circular_behavior) { + Queue queue(3); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + queue.dequeue(); + queue.enqueue(4); + EXPECT_EQ(queue.front(), 2); + EXPECT_EQ(queue.back(), 4); +} + +TEST(TestQueue18, dequeue_from_empty) { + Queue queue(3); + EXPECT_THROW(queue.dequeue(), std::runtime_error); +} + +TEST(TestQueue19, front_from_empty) { + Queue queue(3); + EXPECT_THROW(queue.front(), std::runtime_error); +} + +TEST(TestQueue20, back_from_empty) { + Queue queue(3); + EXPECT_THROW(queue.back(), std::runtime_error); +} + +TEST(TestQueue21, multiple_operations) { + Queue queue(5); + for (int i = 0; i < 10; i++) { + queue.enqueue(i); + if (i % 3 == 0) { + queue.dequeue(); + } + } + ASSERT_NO_THROW(queue); +} + +TEST(TestQueue24, clear_preserves_capacity) { + Queue queue(7); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + int old_capacity = queue.capacity(); + queue.clear(); + EXPECT_EQ(queue.capacity(), old_capacity); + EXPECT_TRUE(queue.empty()); +} + +TEST(TestQueue25, size_after_multiple_dequeues) { + Queue queue(5); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + queue.dequeue(); + queue.dequeue(); + EXPECT_EQ(queue.size(), 1); + EXPECT_EQ(queue.front(), 3); +} + +TEST(TestStack1, can_create_stack) { + + Stack stack(10); + + ASSERT_NO_THROW(stack); +} + +TEST(TestStack2, can_push) { + + Stack stack(3); + stack.push(17); + + ASSERT_NO_THROW(stack); +} +TEST(TestStack3, can_pop) { + + Stack stack(3); + stack.push(17); + stack.push(3); + stack.push(2); + stack.pop(); + + ASSERT_NO_THROW(stack); +} + +TEST(TestStack4, can_take_top) { + + Stack stack(3); + stack.push(17); + stack.push(3); + stack.push(2); + + EXPECT_EQ(stack.top(), 2); +} + +TEST(TestStack5, check_is_full) { + + Stack stack(3); + stack.push(17); + stack.push(3); + stack.push(2); + + EXPECT_EQ(stack.is_full(), 1); +} + +TEST(TestStack6, check_is_empty) { + + Stack stack(3); + + EXPECT_EQ(stack.is_empty(), 1); +} + + +TEST(TestStack7, can_clear_steck) { + + Stack stack(3); + stack.push(17); + stack.push(3); + stack.push(2); + + stack.clear(); + + EXPECT_EQ(stack.is_empty(), 1); +} + + +TEST(TestStack8, can_puch_after_clear) { + + Stack stack(3); + stack.push(17); + stack.push(3); + stack.push(2); + + stack.clear(); + + stack.push(5); + + EXPECT_EQ(stack.top(), 5); +} +//TEST(TestBrackets1, 1) { +// EXPECT_TRUE(check_algorithms("()")); +//} +// +//TEST(TestBrackets2, 2) { +// EXPECT_TRUE(check_algorithms("(){}[]")); +//} +// +//TEST(TestBrackets3, 3) { +// EXPECT_TRUE(check_algorithms("([{}])")); +//} +// +//TEST(TestBrackets4, 4) { +// EXPECT_FALSE(check_algorithms("(]")); +//} +// +//TEST(TestBrackets5, 5) { +// EXPECT_FALSE(check_algorithms("({[}])")); +//} +// +//TEST(TestBrackets6, 6) { +// EXPECT_TRUE(check_algorithms("")); +//} +// +//TEST(TestBrackets7, 7) { +// EXPECT_FALSE(check_algorithms("(((")); +//} +// +//TEST(TestBrackets8, 8) { +// EXPECT_FALSE(check_algorithms(")))")); +//} + + + From 944c779608ba5cd17d5997d8a73b16e9b51b8111 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 11 Nov 2025 22:58:59 +0300 Subject: [PATCH 2/6] Delete lib_list directory --- lib_list/CMakeLists.txt | 1 - lib_list/List.cpp | 0 lib_list/List.h | 279 ---------------------------------------- 3 files changed, 280 deletions(-) delete mode 100644 lib_list/CMakeLists.txt delete mode 100644 lib_list/List.cpp delete mode 100644 lib_list/List.h diff --git a/lib_list/CMakeLists.txt b/lib_list/CMakeLists.txt deleted file mode 100644 index 148b539a..00000000 --- a/lib_list/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -create_project_lib(List) \ No newline at end of file diff --git a/lib_list/List.cpp b/lib_list/List.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/lib_list/List.h b/lib_list/List.h deleted file mode 100644 index a8a64869..00000000 --- a/lib_list/List.h +++ /dev/null @@ -1,279 +0,0 @@ -#pragma once - - -template -struct Node { - - T value; - Node* next; - Node(T value_, Node* next_ = nullptr); -}; - -template -Node::Node(T value_, Node* next_) : value(value_), next(next_) {} - -template -class List { - - Node* _head; - Node* _tail; - int _count; - -public: - - - class Iterator { - - - Node* current; - - public: - - Iterator() : current(nullptr) {} - Iterator(Node* node) : current(node) {} - - Iterator& operator=(const Iterator& other) { - current = other.current; - return *this; - } - - T& operator*() { - return current->value; - } - - Iterator& operator++() { - if (current != nullptr) { - current = current->next; - } - return *this; - } - - Iterator operator++(int) { - Iterator temp = *this; - ++(*this); - return temp; - } - - bool operator==(const Iterator& other) const { - return current == other.current; - } - - bool operator!=(const Iterator& other) const { - return current != other.current; - } - - }; - - - - List() : _head(nullptr), _tail(nullptr), _count(0) {} - List(const List& other) { - _head = nullptr; - _tail = nullptr; - _count = 0; - - Node* current = other._head; - while (current != nullptr) { - push_back(current->value); - current = current->next; - } - } - - ~List() { - while (!is_empty()) { - pop_front(); - } - } - - List& operator=(const List& other) { - if (this != &other) { - while (!is_empty()) { - pop_front(); - } - - Node* current = other._head; - while (current != nullptr) { - push_back(current->value); - current = current->next; - } - } - return *this; - } - - void push_front(const T& val); - void push_back(const T& val); - void insert(int pos, const T& val); - void insert(Node* node, const T& val); - - void pop_front(); - void pop_back(); - T front() const; - T back() const; - void erase(Node* node); - - bool is_empty(); - Node* find(const T& val); - int size() const { return _count; } - -}; - -template -void List::push_front(const T& val) { - - Node* node = new Node(val, _head); - node->next = _head; - if (is_empty()) _tail = node; - _head = node; - _count++; - -} - -template -bool List::is_empty() { - return _head == nullptr; -} - -template -void List::push_back(const T& val) { - - Node* node = new Node(val); - // _tail->next = node; // -, - if (is_empty()) { - _head = node; - _tail = node; - _count++; - return; - } - - _tail->next = node; - _tail = node; - ++_count; - // nullptr - // (val) ---> Null - // node - -} - -template -void List::insert(Node* node, const T& val) { - if (node == nullptr) throw ("Error"); - Node* new_node = new Node(val, node->next); - node->next = new_node; - if (_tail == node) { - _tail = new_node; - } - _count++; -} - -template -void List::insert(int pos, const T& val) { - if (pos < 0 || pos >= _count) throw ("Error"); - - if (pos == 0) { - push_front(val); - } - else if (pos == _count - 1) { - push_back(val); - } - else { - int cur_pos = 0; - Node* cur = _head; - while (cur != nullptr) { - if (cur_pos == pos - 1) break; - cur_pos++; - cur = cur->next; - } - - if (cur != nullptr) { - Node* new_node = new Node(val, cur->next); - cur->next = new_node; - _count++; - } - } -} - -template -Node* List::find(const T& val) { - Node* current = _head; - while (current != nullptr) { - if (current->value == val) { - return current; - } - current = current->next; - } - return nullptr; -} - -template -void List::pop_front() { - if (is_empty()) throw ("List is empty"); - - Node* temp = _head; - _head = _head->next; - delete temp; - _count--; - - if (_head == nullptr) { - _tail = nullptr; - } -} - -template -void List::pop_back() { - if (is_empty()) throw ("List is empty"); - - if (_head == _tail) { - delete _head; - _head = nullptr; - _tail = nullptr; - _count = 0; - return; - } - - Node* current = _head; - while (current->next != _tail) { - current = current->next; - } - - delete _tail; - _tail = current; - _tail->next = nullptr; - _count--; -} - -template -T List::front() const { - if (is_empty()) throw ("List is empty"); - return _head->value; -} - -template -T List::back() const { - if (is_empty()) throw ("List is empty"); - return _tail->value; -} - - -template -void List::erase(Node* node) { - if (node == nullptr || is_empty()) return; - - if (node == _head) { - pop_front(); - return; - } - - Node* prev = _head; - while (prev != nullptr && prev->next != node) { - prev = prev->next; - } - - if (prev != nullptr) { - prev->next = node->next; - if (node == _tail) { - _tail = prev; - } - delete node; - _count--; - } -} From efdaad92eaa3fcb30aa53c5f3ba6fef9b369bf2f Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 11 Nov 2025 22:59:10 +0300 Subject: [PATCH 3/6] Delete lib_queue directory --- lib_queue/CMakeLists.txt | 1 - lib_queue/queue.cpp | 0 lib_queue/queue.h | 173 --------------------------------------- 3 files changed, 174 deletions(-) delete mode 100644 lib_queue/CMakeLists.txt delete mode 100644 lib_queue/queue.cpp delete mode 100644 lib_queue/queue.h diff --git a/lib_queue/CMakeLists.txt b/lib_queue/CMakeLists.txt deleted file mode 100644 index d918c89d..00000000 --- a/lib_queue/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -create_project_lib(Queue) diff --git a/lib_queue/queue.cpp b/lib_queue/queue.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/lib_queue/queue.h b/lib_queue/queue.h deleted file mode 100644 index 9611de36..00000000 --- a/lib_queue/queue.h +++ /dev/null @@ -1,173 +0,0 @@ -template -class Queue { - T* _data; - int _head; - int _count; - int _capacity; - -public: - - Queue(int capacity = 15); - Queue(const Queue& other); - ~Queue(); - - Queue& operator=(const Queue& other); - Queue& operator=(Queue&& other) noexcept; - void enqueue(const T& value); - T dequeue(); - T& front(); - const T& front() const; - T& back(); - const T& back() const; - bool empty() const; - bool full() const; - int size() const; - int capacity() const; - void clear(); - int tail() const; - void resize(int newCapacity); - -}; - -template -Queue::Queue(int capacity) : _data(new T[capacity]), _head(0), _count(0), _capacity(capacity){} - -template -Queue::Queue(const Queue& other) : _data(new T[other._capacity]), _head(other._head), _count(other._count), _capacity(other._capacity) { - for (int i = 0; i < _count; i++) { - _data[(i + _head) % _capacity] = other._data[(i + other._head) % other._capacity]; - } -} - -template -Queue::~Queue() { - delete[] _data; -} - -template -Queue& Queue::operator=(const Queue& other) { - if (this != &other) { - delete[] _data; - _capacity = other._capacity; - _head = other._head; - _count = other._count; - _data = new T[_capacity]; - - for (int i = 0; i < _count; i++) { - _data[(i + _head) % _capacity] = other._data[(i + other._head) % other._capacity]; - } - } - return *this; -} - -template -Queue& Queue::operator=(Queue&& other) noexcept { - if (this != &other) { - delete[] _data; - _data = other._data; - _head = other._head; - _count = other._count; - _capacity = other._capacity; - - other._data = nullptr; - other._head = 0; - other._count = 0; - other._capacity = 0; - } - return *this; -} - -template -void Queue::enqueue(const T& value) { - if (full()) { - resize(_capacity * 2); - } - _data[tail()] = value; - _count++; -} - -template -T Queue::dequeue() { - if (empty()) { - throw std::runtime_error("Queue is empty"); - } - T value = std::move(_data[_head]); - _head = (_head + 1) % _capacity; - _count--; - return value; -} - -template -T& Queue::front() { - if (empty()) { - throw std::runtime_error("Queue is empty"); - } - return _data[_head]; -} - -template -const T& Queue::front() const { - if (empty()) { - throw std::runtime_error("Queue is empty"); - } - return _data[_head]; -} - -template -T& Queue::back() { - if (empty()) { - throw std::runtime_error("Queue is empty"); - } - return _data[tail() - 1]; -} - -template -const T& Queue::back() const { - if (empty()) { - throw std::runtime_error("Queue is empty"); - } - return _data[tail() - 1]; -} - -template -bool Queue::empty() const { - return _count == 0; -} - -template -bool Queue::full() const { - return _count == _capacity; -} - -template -int Queue::size() const { - return _count; -} - -template -int Queue::capacity() const { - return _capacity; -} - -template -void Queue::clear() { - _head = 0; - _count = 0; -} - -template -int Queue::tail() const { - return (_head + _count) % _capacity; -} - -template -void Queue::resize(int newCapacity) { - T* newData = new T[newCapacity]; - for (int i = 0; i < _count; i++) { - newData[i] = std::move(_data[(_head + i) % _capacity]); - } - delete[] _data; - _data = newData; - _head = 0; - _capacity = newCapacity; -} \ No newline at end of file From daf511bf61a597856531d52fdef03c4902810090 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 11 Nov 2025 22:59:21 +0300 Subject: [PATCH 4/6] Delete lib_stack directory --- lib_stack/CMakeLists.txt | 1 - lib_stack/Stack.cpp | 0 lib_stack/Stack.h | 75 ---------------------------------------- 3 files changed, 76 deletions(-) delete mode 100644 lib_stack/CMakeLists.txt delete mode 100644 lib_stack/Stack.cpp delete mode 100644 lib_stack/Stack.h diff --git a/lib_stack/CMakeLists.txt b/lib_stack/CMakeLists.txt deleted file mode 100644 index b6bedb01..00000000 --- a/lib_stack/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -create_project_lib(Stack) \ No newline at end of file diff --git a/lib_stack/Stack.cpp b/lib_stack/Stack.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/lib_stack/Stack.h b/lib_stack/Stack.h deleted file mode 100644 index 9921ee12..00000000 --- a/lib_stack/Stack.h +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once -#include -template -class Stack { - -private: - - T* _data; - int _size, _top; - -public: - - Stack(int size); - Stack(); - void push(const T& val); - T pop(); - inline T top(); - inline bool is_empty() const noexcept; - inline bool is_full() const noexcept; - void clear(); - -}; - -template -inline bool Stack::is_empty() const noexcept { - - return _top == 0; - -} - -template -inline bool Stack::is_full() const noexcept { - - return _top == _size; - -} - -template -Stack::Stack(int size) : _data(new T[size]), _size(size), _top(0) {}; -template -Stack::Stack() : _data(nullptr), _size(0), _top(0) {}; - -template -void Stack::push(const T& val) { - - if (is_full()) { - throw std::logic_error("Queue is full"); - } - _data[++_top] = val; - -} - -template -T Stack::pop() { - - if (is_empty()) { - throw std::logic_error("Stack is empty"); - } - --_top; - -} -template -inline T Stack::top() { - if (is_empty()) { - throw std::logic_error("Stack is empty"); - } - return _data[_top]; -} - -template -void Stack::clear() { - - _top = 0; - -} \ No newline at end of file From e71a7eecb8b315e56ca1dd29400d711b88460855 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 16 Dec 2025 23:56:57 +0300 Subject: [PATCH 5/6] New DSU + tests + function countIslands --- CMakeLists.txt | 3 - lib_DSU/DSU.h | 97 ++++++------- main/main.cpp | 97 +++++++++---- tests/test_easy_example.cpp | 280 ------------------------------------ 4 files changed, 110 insertions(+), 367 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6596642..f20a8d11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,9 +15,6 @@ include(cmake/function.cmake) # подхватываем функции, add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example add_subdirectory(lib_DSU) -add_subdirectory(lib_list) -add_subdirectory(lib_stack) -add_subdirectory(lib_queue) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_DSU/DSU.h b/lib_DSU/DSU.h index 6b6efbcd..9bd27050 100644 --- a/lib_DSU/DSU.h +++ b/lib_DSU/DSU.h @@ -1,57 +1,46 @@ class DSU { - - int* _parent; - size_t _size; - int* _rank; + int* _parent; + int* _rank; + size_t _size; public: - - DSU(size_t size); - void unite(int, int); - int find(int); - ~DSU(); - -}; - -DSU::DSU(size_t size) : _size(size) { - - _parent = new int[size]; - _rank = new int[size]; - for (int i = 0; i < _size; i++) { - _parent[i] = i; - } - - -} - -DSU::~DSU() { - - delete[] _parent; - -} - -int DSU::find(int x) { - - if (_parent[x] == x) { - return x; - } - else { - return find(_parent[x]); - } - -} - - -void DSU::unite(int x1, int x2) { - - if (_rank[x1] < _rank[x2]) { - _parent[x1] = find(x2); - } - else { - if (_rank[x1] == _rank[x2]) { - _rank[x1]++; - } - _parent[x2] = find(x1); - } - -} \ No newline at end of file + DSU(size_t size) : _size(size) { + _parent = new int[size]; + _rank = new int[size]; + for (int i = 0; i < size; i++) { + _parent[i] = i; + _rank[i] = 0; + } + } + + ~DSU() { + delete[] _parent; + delete[] _rank; + } + + int find(int x) { + if (x < 0 || x >= _size) return -1; + if (_parent[x] != x) { + _parent[x] = find(_parent[x]); + } + return _parent[x]; + } + + void unite(int x, int y) { + int rootX = find(x); + int rootY = find(y); + + if (rootX == rootY || rootX == -1 || rootY == -1) return; + + if (_rank[rootX] < _rank[rootY]) { + _parent[rootX] = rootY; + } + else if (_rank[rootX] > _rank[rootY]) { + _parent[rootY] = rootX; + } + else { + _parent[rootY] = rootX; + _rank[rootX]++; + } + } +}; \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index 217f8971..5cb733a1 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,37 +1,74 @@ // Copyright 2024 Marina Usova -#define EASY_EXAMPLE -#ifdef EASY_EXAMPLE + #include -#include -#include "../lib_easy_example/easy_example.h" +#include "DSU.h" +#include -int main() { - int a, b; - float result; - - a = 1; b = 4; - - try { - result = division(a, b); - std::cout << a << " / " << b << " = " - << std::setprecision(2) << result << std::endl; - } catch (std::exception err) { - std::cerr << err.what() << std::endl; - } - - a = 1; b = 0; - - try { - result = division(a, b); - std::cout << a << " / " << b << " = " - << std::setprecision(2) << result << std::endl; - } catch (std::exception err) { - std::cerr << err.what() << std::endl; - } - - return 0; +int countIslands(std::vector>& grid) { + if (grid.empty() || grid[0].empty()) return 0; + + int rows = grid.size(); + int cols = grid[0].size(); + + DSU dsu(rows * cols); + + int directions[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (grid[i][j] == 1) { + int currentIndex = i * cols + j; + + for (int k = 0; k < 4; k++) { + int newRow = i + directions[k][0]; + int newCol = j + directions[k][1]; + + if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == 1) { + int neighborIndex = newRow * cols + newCol; + dsu.unite(currentIndex, neighborIndex); + } + } + } + } + } + + int totalCells = rows * cols; + bool* seen = new bool[totalCells]; + for (int i = 0; i < totalCells; i++) { + seen[i] = false; + } + + int count = 0; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (grid[i][j] == 1) { + int index = i * cols + j; + int root = dsu.find(index); + if (root != -1 && !seen[root]) { + seen[root] = true; + count++; + } + } + } + } + + delete[] seen; + return count; } -#endif // EASY_EXAMPLE +int main() { + std::vector> grid = { + {0, 1, 0, 0, 1}, + {0, 1, 1, 0, 1}, + {1, 1, 0, 1, 1}, + {0, 0, 0, 0, 1}, + {1, 0, 1, 1, 1} + }; + + int islandsCount = countIslands(grid); + std::cout << "Number of islands: " << islandsCount << std::endl; + + return 0; +} diff --git a/tests/test_easy_example.cpp b/tests/test_easy_example.cpp index fd9a8aee..01aab1a8 100644 --- a/tests/test_easy_example.cpp +++ b/tests/test_easy_example.cpp @@ -3,290 +3,10 @@ #include #include "../lib_easy_example/easy_example.h" #define EPSILON 0.000001 -#include "Queue.h" -#include "Stack.h" -#include "List.h" -TEST(TestLibLib, can_iterate) { - List list; - for (int i = 0; i < 10; i++) { - - list.push_back(i + 1); - - } - // Node* cur = list._head; - - - - -} - -TEST(TestQueue1, can_create_queue) { - Queue queue(10); - ASSERT_NO_THROW(queue); -} - -TEST(TestQueue2, can_enqueue) { - Queue queue(3); - queue.enqueue(17); - ASSERT_NO_THROW(queue); -} - -TEST(TestQueue3, can_dequeue) { - Queue queue(3); - queue.enqueue(17); - queue.enqueue(3); - queue.enqueue(2); - int result = queue.dequeue(); - ASSERT_NO_THROW(queue); - EXPECT_EQ(result, 17); -} - -TEST(TestQueue4, can_take_front) { - Queue queue(3); - queue.enqueue(17); - queue.enqueue(3); - queue.enqueue(2); - EXPECT_EQ(queue.front(), 17); -} - -TEST(TestQueue6, check_is_empty) { - Queue queue(3); - EXPECT_EQ(queue.empty(), true); -} - -TEST(TestQueue7, check_is_full) { - Queue queue(3); - queue.enqueue(17); - queue.enqueue(3); - queue.enqueue(2); - EXPECT_EQ(queue.full(), true); -} - -TEST(TestQueue8, can_clear_queue) { - Queue queue(3); - queue.enqueue(17); - queue.enqueue(3); - queue.enqueue(2); - queue.clear(); - EXPECT_EQ(queue.empty(), true); -} - -TEST(TestQueue9, can_enqueue_after_clear) { - Queue queue(3); - queue.enqueue(17); - queue.enqueue(3); - queue.enqueue(2); - queue.clear(); - queue.enqueue(5); - EXPECT_EQ(queue.front(), 5); -} - -TEST(TestQueue10, can_get_size) { - Queue queue(3); - queue.enqueue(17); - queue.enqueue(3); - EXPECT_EQ(queue.size(), 2); -} - -TEST(TestQueue11, can_get_capacity) { - Queue queue(7); - EXPECT_EQ(queue.capacity(), 7); -} - -TEST(TestQueue12, fifo_order) { - Queue queue(3); - queue.enqueue(1); - queue.enqueue(2); - queue.enqueue(3); - EXPECT_EQ(queue.dequeue(), 1); - EXPECT_EQ(queue.dequeue(), 2); - EXPECT_EQ(queue.dequeue(), 3); -} - - - -TEST(TestQueue16, auto_resize) { - Queue queue(2); - queue.enqueue(1); - queue.enqueue(2); - queue.enqueue(3); - EXPECT_EQ(queue.size(), 3); - EXPECT_EQ(queue.capacity(), 4); -} - -TEST(TestQueue17, circular_behavior) { - Queue queue(3); - queue.enqueue(1); - queue.enqueue(2); - queue.enqueue(3); - queue.dequeue(); - queue.enqueue(4); - EXPECT_EQ(queue.front(), 2); - EXPECT_EQ(queue.back(), 4); -} - -TEST(TestQueue18, dequeue_from_empty) { - Queue queue(3); - EXPECT_THROW(queue.dequeue(), std::runtime_error); -} - -TEST(TestQueue19, front_from_empty) { - Queue queue(3); - EXPECT_THROW(queue.front(), std::runtime_error); -} - -TEST(TestQueue20, back_from_empty) { - Queue queue(3); - EXPECT_THROW(queue.back(), std::runtime_error); -} - -TEST(TestQueue21, multiple_operations) { - Queue queue(5); - for (int i = 0; i < 10; i++) { - queue.enqueue(i); - if (i % 3 == 0) { - queue.dequeue(); - } - } - ASSERT_NO_THROW(queue); -} - -TEST(TestQueue24, clear_preserves_capacity) { - Queue queue(7); - queue.enqueue(1); - queue.enqueue(2); - queue.enqueue(3); - int old_capacity = queue.capacity(); - queue.clear(); - EXPECT_EQ(queue.capacity(), old_capacity); - EXPECT_TRUE(queue.empty()); -} - -TEST(TestQueue25, size_after_multiple_dequeues) { - Queue queue(5); - queue.enqueue(1); - queue.enqueue(2); - queue.enqueue(3); - queue.dequeue(); - queue.dequeue(); - EXPECT_EQ(queue.size(), 1); - EXPECT_EQ(queue.front(), 3); -} - -TEST(TestStack1, can_create_stack) { - - Stack stack(10); - - ASSERT_NO_THROW(stack); -} - -TEST(TestStack2, can_push) { - - Stack stack(3); - stack.push(17); - - ASSERT_NO_THROW(stack); -} -TEST(TestStack3, can_pop) { - - Stack stack(3); - stack.push(17); - stack.push(3); - stack.push(2); - stack.pop(); - - ASSERT_NO_THROW(stack); -} - -TEST(TestStack4, can_take_top) { - - Stack stack(3); - stack.push(17); - stack.push(3); - stack.push(2); - - EXPECT_EQ(stack.top(), 2); -} - -TEST(TestStack5, check_is_full) { - - Stack stack(3); - stack.push(17); - stack.push(3); - stack.push(2); - - EXPECT_EQ(stack.is_full(), 1); -} - -TEST(TestStack6, check_is_empty) { - - Stack stack(3); - - EXPECT_EQ(stack.is_empty(), 1); -} - - -TEST(TestStack7, can_clear_steck) { - - Stack stack(3); - stack.push(17); - stack.push(3); - stack.push(2); - - stack.clear(); - - EXPECT_EQ(stack.is_empty(), 1); -} - - -TEST(TestStack8, can_puch_after_clear) { - - Stack stack(3); - stack.push(17); - stack.push(3); - stack.push(2); - - stack.clear(); - - stack.push(5); - - EXPECT_EQ(stack.top(), 5); -} -//TEST(TestBrackets1, 1) { -// EXPECT_TRUE(check_algorithms("()")); -//} -// -//TEST(TestBrackets2, 2) { -// EXPECT_TRUE(check_algorithms("(){}[]")); -//} -// -//TEST(TestBrackets3, 3) { -// EXPECT_TRUE(check_algorithms("([{}])")); -//} -// -//TEST(TestBrackets4, 4) { -// EXPECT_FALSE(check_algorithms("(]")); -//} -// -//TEST(TestBrackets5, 5) { -// EXPECT_FALSE(check_algorithms("({[}])")); -//} -// -//TEST(TestBrackets6, 6) { -// EXPECT_TRUE(check_algorithms("")); -//} -// -//TEST(TestBrackets7, 7) { -// EXPECT_FALSE(check_algorithms("(((")); -//} -// -//TEST(TestBrackets8, 8) { -// EXPECT_FALSE(check_algorithms(")))")); -//} From 3f3210d4b9e504f00b41669777e4452a0e34ab8c Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Wed, 17 Dec 2025 08:11:35 +0300 Subject: [PATCH 6/6] -------- --- lib_DSU/DSU.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib_DSU/DSU.h b/lib_DSU/DSU.h index 9bd27050..c0c908de 100644 --- a/lib_DSU/DSU.h +++ b/lib_DSU/DSU.h @@ -4,6 +4,7 @@ class DSU { size_t _size; public: + DSU(size_t size) : _size(size) { _parent = new int[size]; _rank = new int[size];