From d90700be9542df0f58ab938461736c6adca88f45 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 4 Nov 2025 15:12:27 +0300 Subject: [PATCH 01/13] queue_realization --- lib_queue/CMakeLists.txt | 1 + lib_queue/queue.cpp | 0 lib_queue/queue.h | 173 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 lib_queue/CMakeLists.txt create mode 100644 lib_queue/queue.cpp create mode 100644 lib_queue/queue.h 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 From 1e808987ce7ba7dafbed278581a24c684fca79e6 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 4 Nov 2025 16:31:42 +0300 Subject: [PATCH 02/13] stack_realization --- lib_stack/CMakeLists.txt | 1 + lib_stack/Stack.cpp | 0 lib_stack/Stack.h | 75 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib_stack/CMakeLists.txt create mode 100644 lib_stack/Stack.cpp create mode 100644 lib_stack/Stack.h 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 From b9c8821db2089f88cdc21cbd6201e5ec4299f17d Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 4 Nov 2025 17:06:49 +0300 Subject: [PATCH 03/13] list_realization --- lib_list/CMakeLists.txt | 1 + lib_list/List.cpp | 0 lib_list/List.h | 253 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 lib_list/CMakeLists.txt create mode 100644 lib_list/List.cpp create mode 100644 lib_list/List.h 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..c6e4f1bb --- /dev/null +++ b/lib_list/List.h @@ -0,0 +1,253 @@ +#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(); + + Iterator& operator = (const Iterator& other); + T& operator*(); + Iterator operator++(); // x++; + Iterator& operator++(); // ++x; + + }; + + + + 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 c0d08407e03d470eb174de59fb5c1388e60428be Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 4 Nov 2025 18:05:16 +0300 Subject: [PATCH 04/13] List_with_Iterator --- lib_list/List.h | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index c6e4f1bb..a8a64869 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -29,12 +29,38 @@ class List { public: - Iterator(); + Iterator() : current(nullptr) {} + Iterator(Node* node) : current(node) {} - Iterator& operator = (const Iterator& other); - T& operator*(); - Iterator operator++(); // x++; - Iterator& operator++(); // ++x; + 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; + } }; From 0a4654bd5a2ef8da206fd0fc8ae48f06ba48aaf6 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 11 Nov 2025 22:59:35 +0300 Subject: [PATCH 05/13] 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 c87fef044530e63e820c1702f61178a20b5f4bfb Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 11 Nov 2025 22:59:48 +0300 Subject: [PATCH 06/13] 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 37a15d75361d0d8bb93975ee8c88529808da05a3 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 11 Nov 2025 23:00:18 +0300 Subject: [PATCH 07/13] 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 281b9abe07115872866058576fdb590f32856469 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Wed, 19 Nov 2025 12:02:09 +0300 Subject: [PATCH 08/13] Tests for List --- tests/tests_List.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/tests_List.cpp diff --git a/tests/tests_List.cpp b/tests/tests_List.cpp new file mode 100644 index 00000000..337b8f6c --- /dev/null +++ b/tests/tests_List.cpp @@ -0,0 +1,50 @@ +#include +#include "List.h" + + +TEST(TestList1, can_iterate) { + + List list; + ASSERT_NO_THROW(list); + + +} + +TEST(TestList2, can_push_front) { + + List list; + list.push_front(20); + EXPECT_EQ(list.front(), 20); + + +} + +TEST(TestList3, can_push_back) { + + List list; + list.push_front(20); + list.push_back(10); + EXPECT_EQ(list.back(), 10); + + +} + + +TEST(TestList4, can_delete_when_empty) { + + List list; + + ASSERT_ANY_THROW(list.pop_front()); + + +} + +TEST(TestList5, can_delete_when_empty) { + + List list; + + ASSERT_ANY_THROW(list.pop_back()); + + +} + From f0e555d828b2bef2042c1ac576fd1811246731c0 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Sun, 30 Nov 2025 14:17:34 +0300 Subject: [PATCH 09/13] add Algorithms module with stack --- CMakeLists.txt | 3 ++- lib_algorithms/Algorithms.cpp | 26 ++++++++++++++++++++++++++ lib_algorithms/Algorithms.h | 2 ++ lib_algorithms/CMakeLists.txt | 3 +++ tests/test_algorithms.cpp | 0 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib_algorithms/Algorithms.cpp create mode 100644 lib_algorithms/Algorithms.h create mode 100644 lib_algorithms/CMakeLists.txt create mode 100644 tests/test_algorithms.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fb5abf3a..8e09a45d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,8 @@ include(cmake/function.cmake) # подхватываем функции, # и для создания исполняемого проекта в отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example - +add_subdirectory(lib_Stack) +add_subdirectory(lib_Algorithms) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_algorithms/Algorithms.cpp b/lib_algorithms/Algorithms.cpp new file mode 100644 index 00000000..945579f8 --- /dev/null +++ b/lib_algorithms/Algorithms.cpp @@ -0,0 +1,26 @@ +#include "Algorithms.h" + +bool check_balanced_brackets(const std::string& expression) { + Stack stack(expression.length()); + + for (char ch : expression) { + if (ch == '(' || ch == '{' || ch == '[') { + stack.push(ch); + } + else if (ch == ')' || ch == '}' || ch == ']') { + + if (stack.is_empty()) { + return false; + } + + char top_char = stack.pop(); + + if ((ch == ')' && top_char != '(') || + (ch == '}' && top_char != '{') || + (ch == ']' && top_char != '[')) { + return false; + } + } + } + return stack.is_empty(); +} \ No newline at end of file diff --git a/lib_algorithms/Algorithms.h b/lib_algorithms/Algorithms.h new file mode 100644 index 00000000..0550627b --- /dev/null +++ b/lib_algorithms/Algorithms.h @@ -0,0 +1,2 @@ +#include "Stack.h" +bool check_balanced_brackets(const std::string& expression); \ No newline at end of file diff --git a/lib_algorithms/CMakeLists.txt b/lib_algorithms/CMakeLists.txt new file mode 100644 index 00000000..a43026e7 --- /dev/null +++ b/lib_algorithms/CMakeLists.txt @@ -0,0 +1,3 @@ +create_project_lib(Algorithms) +add_depend(Algorithms List ../lib_list) +add_depend(Algorithms Stack ../lib_stack) \ No newline at end of file diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp new file mode 100644 index 00000000..e69de29b From a17781cac7fce38aff364c803bfcd1c2bde9d1ee Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Sun, 30 Nov 2025 16:10:45 +0300 Subject: [PATCH 10/13] Doubly list with iterator --- CMakeLists.txt | 3 +- lib_doublyList/CMakeLists.txt | 2 + lib_doublyList/doublyList.cpp | 0 lib_doublyList/doublyList.h | 254 ++++++++++++++++++++++++++++++++++ 4 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 lib_doublyList/CMakeLists.txt create mode 100644 lib_doublyList/doublyList.cpp create mode 100644 lib_doublyList/doublyList.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fb5abf3a..705e977d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,8 @@ include(cmake/function.cmake) # подхватываем функции, # и для создания исполняемого проекта в отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example - +add_subdirectory(lib_list) +add_subdirectory(lib_doublyList) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/lib_doublyList/CMakeLists.txt b/lib_doublyList/CMakeLists.txt new file mode 100644 index 00000000..3356e664 --- /dev/null +++ b/lib_doublyList/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(DoublyList) +add_depend(DoublyList List ../lib_list) \ No newline at end of file diff --git a/lib_doublyList/doublyList.cpp b/lib_doublyList/doublyList.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_doublyList/doublyList.h b/lib_doublyList/doublyList.h new file mode 100644 index 00000000..a9db4023 --- /dev/null +++ b/lib_doublyList/doublyList.h @@ -0,0 +1,254 @@ +#pragma once + +template +struct Node { + T value; + Node* next; + Node* prev; + + Node(T value_, Node* prev_ = nullptr, Node* next_ = nullptr) + : value(value_), prev(prev_), 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; + } + + 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; + } + + Node* get_node() const { return 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() { + clear(); + } + + List& operator=(const List& other) { + if (this != &other) { + clear(); + Node* current = other._head; + while (current != nullptr) { + push_back(current->value); + current = current->next; + } + } + return *this; + } + + Iterator begin() { return Iterator(_head); } + Iterator end() { return Iterator(nullptr); } + + void push_front(const T& val) { + Node* new_node = new Node(val, nullptr, _head); + + if (_head != nullptr) { + _head->prev = new_node; + } + + _head = new_node; + + if (_tail == nullptr) { + _tail = new_node; + } + + _count++; + } + + void push_back(const T& val) { + Node* new_node = new Node(val, _tail, nullptr); + + if (_tail != nullptr) { + _tail->next = new_node; + } + + _tail = new_node; + + if (_head == nullptr) { + _head = new_node; + } + + _count++; + } + + void insert(const Iterator& position, const T& val) { + if (position == begin()) { + push_front(val); + return; + } + + if (position == end()) { + push_back(val); + return; + } + + Node* current_node = position.get_node(); + Node* prev_node = current_node->prev; + + Node* new_node = new Node(val, prev_node, current_node); + + prev_node->next = new_node; + current_node->prev = new_node; + + _count++; + } + + void pop_front() { + if (is_empty()) throw "List is empty"; + + Node* temp = _head; + _head = _head->next; + + if (_head != nullptr) { + _head->prev = nullptr; + } + else { + _tail = nullptr; + } + + delete temp; + _count--; + } + + void pop_back() { + if (is_empty()) throw "List is empty"; + + Node* temp = _tail; + _tail = _tail->prev; + + if (_tail != nullptr) { + _tail->next = nullptr; + } + else { + _head = nullptr; + } + + delete temp; + _count--; + } + + void erase(const Iterator& position) { + if (position == end()) return; + + Node* node_to_delete = position.get_node(); + + if (node_to_delete == _head) { + pop_front(); + return; + } + + if (node_to_delete == _tail) { + pop_back(); + return; + } + + Node* prev_node = node_to_delete->prev; + Node* next_node = node_to_delete->next; + + prev_node->next = next_node; + next_node->prev = prev_node; + + delete node_to_delete; + _count--; + } + + T& front() { + if (is_empty()) throw "List is empty"; + return _head->value; + } + + const T& front() const { + if (is_empty()) throw "List is empty"; + return _head->value; + } + + T& back() { + if (is_empty()) throw "List is empty"; + return _tail->value; + } + + const T& back() const { + if (is_empty()) throw "List is empty"; + return _tail->value; + } + + bool is_empty() const { + return _head == nullptr; + } + + int size() const { + return _count; + } + + void clear() { + while (!is_empty()) { + pop_front(); + } + } + + Iterator find(const T& val) { + Node* current = _head; + while (current != nullptr) { + if (current->value == val) { + return Iterator(current); + } + current = current->next; + } + return end(); + } +}; \ No newline at end of file From 3553f28323095df72d811f67ba746926d4cbaa61 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Mon, 1 Dec 2025 20:31:49 +0300 Subject: [PATCH 11/13] List merge Stack --- lib_stack/CMakeLists.txt | 1 + lib_stack/Stack.cpp | 0 lib_stack/Stack.h | 75 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib_stack/CMakeLists.txt create mode 100644 lib_stack/Stack.cpp create mode 100644 lib_stack/Stack.h 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 From fb9b18ef41f8ef2bd466310309d11f7df0581277 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Wed, 3 Dec 2025 09:58:41 +0300 Subject: [PATCH 12/13] Stack merged list with tests --- lib_doublyList/doublyList.h | 9 --- lib_list/List.h | 5 +- lib_stack/Stack.h | 55 +++++++++++++- tests/test_algorithms.cpp | 76 +++++++++++++++++++ tests/tests_List.cpp | 142 +++++++++++++++++++++++++++++++----- tests/tests_stack.cpp | 127 ++++++++++++++++++++++++++++++++ 6 files changed, 378 insertions(+), 36 deletions(-) create mode 100644 tests/tests_stack.cpp diff --git a/lib_doublyList/doublyList.h b/lib_doublyList/doublyList.h index a9db4023..00d2ff73 100644 --- a/lib_doublyList/doublyList.h +++ b/lib_doublyList/doublyList.h @@ -212,21 +212,12 @@ class List { return _head->value; } - const T& front() const { - if (is_empty()) throw "List is empty"; - return _head->value; - } T& back() { if (is_empty()) throw "List is empty"; return _tail->value; } - const T& back() const { - if (is_empty()) throw "List is empty"; - return _tail->value; - } - bool is_empty() const { return _head == nullptr; } diff --git a/lib_list/List.h b/lib_list/List.h index a8a64869..4746b8df 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -111,7 +111,7 @@ class List { T back() const; void erase(Node* node); - bool is_empty(); + bool is_empty() const; Node* find(const T& val); int size() const { return _count; } @@ -121,7 +121,6 @@ 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++; @@ -129,7 +128,7 @@ void List::push_front(const T& val) { } template -bool List::is_empty() { +bool List::is_empty() const { return _head == nullptr; } diff --git a/lib_stack/Stack.h b/lib_stack/Stack.h index 9921ee12..28708064 100644 --- a/lib_stack/Stack.h +++ b/lib_stack/Stack.h @@ -1,5 +1,6 @@ #pragma once #include +#include "List.h" template class Stack { @@ -46,7 +47,7 @@ void Stack::push(const T& val) { if (is_full()) { throw std::logic_error("Queue is full"); } - _data[++_top] = val; + _data[_top++] = val; } @@ -56,7 +57,7 @@ T Stack::pop() { if (is_empty()) { throw std::logic_error("Stack is empty"); } - --_top; + return _data[--_top]; } template @@ -64,7 +65,7 @@ inline T Stack::top() { if (is_empty()) { throw std::logic_error("Stack is empty"); } - return _data[_top]; + return _data[_top - 1]; } template @@ -72,4 +73,50 @@ void Stack::clear() { _top = 0; -} \ No newline at end of file +} + + +template +class StackList { +private: + List _list; + +public: + StackList() = default; + + void push(const T& val) { + _list.push_back(val); + } + + T pop() { + if (_list.is_empty()) { + throw std::logic_error("Stack is empty"); + } + T value = _list.back(); + _list.pop_back(); + return value; + } + + T& top() { + if (_list.is_empty()) { + throw std::logic_error("Stack is empty"); + } + return _list.back(); + } + + bool is_empty() const { + return _list.is_empty(); + } + + int size() const { + return _list.size(); + } + + void clear() { + _list.clear(); + } + + bool is_full() const { + return false; + } +}; \ No newline at end of file diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index e69de29b..0e4e5f07 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -0,0 +1,76 @@ +#include +#include "Algorithms.h" + +TEST(CheckBalancedBracketsTest1, BalancedSimpleParentheses) { + EXPECT_TRUE(check_balanced_brackets("()")); + EXPECT_TRUE(check_balanced_brackets("(())")); + EXPECT_TRUE(check_balanced_brackets("()()")); +} + +TEST(CheckBalancedBracketsTest2, BalancedMixedBrackets) { + EXPECT_TRUE(check_balanced_brackets("{}")); + EXPECT_TRUE(check_balanced_brackets("[]")); + EXPECT_TRUE(check_balanced_brackets("{[()]}")); + EXPECT_TRUE(check_balanced_brackets("[{()}]")); +} + +TEST(CheckBalancedBracketsTest3, BalancedComplexExpressions) { + EXPECT_TRUE(check_balanced_brackets("({[]})")); + EXPECT_TRUE(check_balanced_brackets("{([])}")); + EXPECT_TRUE(check_balanced_brackets("([{}])")); +} + +TEST(CheckBalancedBracketsTest4, BalancedWithOtherCharacters) { + EXPECT_TRUE(check_balanced_brackets("(a + b)")); + EXPECT_TRUE(check_balanced_brackets("{a * [b + c]}")); + EXPECT_TRUE(check_balanced_brackets("if (x > 0) { y = x * 2; }")); +} + +TEST(CheckBalancedBracketsTest5, UnbalancedMissingClosing) { + EXPECT_FALSE(check_balanced_brackets("(")); + EXPECT_FALSE(check_balanced_brackets("{")); + EXPECT_FALSE(check_balanced_brackets("[")); + EXPECT_FALSE(check_balanced_brackets("(()")); +} + +TEST(CheckBalancedBracketsTest6, UnbalancedMissingOpening) { + EXPECT_FALSE(check_balanced_brackets(")")); + EXPECT_FALSE(check_balanced_brackets("}")); + EXPECT_FALSE(check_balanced_brackets("]")); + EXPECT_FALSE(check_balanced_brackets("())")); +} + +TEST(CheckBalancedBracketsTest7, UnbalancedWrongOrder) { + EXPECT_FALSE(check_balanced_brackets(")(")); + EXPECT_FALSE(check_balanced_brackets("][")); + EXPECT_FALSE(check_balanced_brackets("}{")); +} + +TEST(CheckBalancedBracketsTest8, UnbalancedMismatchedTypes) { + EXPECT_FALSE(check_balanced_brackets("(]")); + EXPECT_FALSE(check_balanced_brackets("{]")); + EXPECT_FALSE(check_balanced_brackets("[}")); + EXPECT_FALSE(check_balanced_brackets("{)")); +} + +TEST(CheckBalancedBracketsTest9, UnbalancedNestedMismatch) { + EXPECT_FALSE(check_balanced_brackets("{[}]")); + EXPECT_FALSE(check_balanced_brackets("({[}])")); + EXPECT_FALSE(check_balanced_brackets("[(])")); +} + +TEST(CheckBalancedBracketsTest10, EmptyString) { + EXPECT_TRUE(check_balanced_brackets("")); +} + +TEST(CheckBalancedBracketsTest11, NoBrackets) { + EXPECT_TRUE(check_balanced_brackets("abc")); + EXPECT_TRUE(check_balanced_brackets("123")); + EXPECT_TRUE(check_balanced_brackets("hello world")); +} + +TEST(CheckBalancedBracketsTest12, MixedBalanced) { + EXPECT_TRUE(check_balanced_brackets("([]){()}")); + EXPECT_TRUE(check_balanced_brackets("({[]})()")); + EXPECT_TRUE(check_balanced_brackets("(([]){})")); +} \ No newline at end of file diff --git a/tests/tests_List.cpp b/tests/tests_List.cpp index 337b8f6c..071b6f5b 100644 --- a/tests/tests_List.cpp +++ b/tests/tests_List.cpp @@ -1,50 +1,152 @@ #include #include "List.h" +TEST(ListTest1, DefaultConstructor) { + List list; + EXPECT_TRUE(list.is_empty()); + EXPECT_EQ(list.size(), 0); +} -TEST(TestList1, can_iterate) { +TEST(ListTest2, PushFrontSingle) { + List list; + list.push_front(42); + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(list.size(), 1); + EXPECT_EQ(list.front(), 42); + EXPECT_EQ(list.back(), 42); +} - List list; - ASSERT_NO_THROW(list); +TEST(ListTest3, PushBackSingle) { + List list; + list.push_back(42); + EXPECT_FALSE(list.is_empty()); + EXPECT_EQ(list.size(), 1); + EXPECT_EQ(list.front(), 42); + EXPECT_EQ(list.back(), 42); +} +TEST(ListTest4, PushFrontMultiple) { + List list; + list.push_front(3); + list.push_front(2); + list.push_front(1); + EXPECT_EQ(list.size(), 3); + EXPECT_EQ(list.front(), 1); + EXPECT_EQ(list.back(), 3); +} +TEST(ListTest5, PushBackMultiple) { + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + EXPECT_EQ(list.size(), 3); + EXPECT_EQ(list.front(), 1); + EXPECT_EQ(list.back(), 3); } -TEST(TestList2, can_push_front) { +TEST(ListTest6, MixedPushOperations) { + List list; + list.push_front(2); + list.push_back(3); + list.push_front(1); + list.push_back(4); + EXPECT_EQ(list.size(), 4); + EXPECT_EQ(list.front(), 1); + EXPECT_EQ(list.back(), 4); +} + +TEST(ListTest7, PopFront) { + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); - List list; - list.push_front(20); - EXPECT_EQ(list.front(), 20); + list.pop_front(); + EXPECT_EQ(list.size(), 2); + EXPECT_EQ(list.front(), 2); + EXPECT_EQ(list.back(), 3); + list.pop_front(); + EXPECT_EQ(list.size(), 1); + EXPECT_EQ(list.front(), 3); + EXPECT_EQ(list.back(), 3); + list.pop_front(); + EXPECT_TRUE(list.is_empty()); } -TEST(TestList3, can_push_back) { +TEST(ListTest8, PopBack) { + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); - List list; - list.push_front(20); - list.push_back(10); - EXPECT_EQ(list.back(), 10); + list.pop_back(); + EXPECT_EQ(list.size(), 2); + EXPECT_EQ(list.front(), 1); + EXPECT_EQ(list.back(), 2); + list.pop_back(); + EXPECT_EQ(list.size(), 1); + EXPECT_EQ(list.front(), 1); + EXPECT_EQ(list.back(), 1); + list.pop_back(); + EXPECT_TRUE(list.is_empty()); } +TEST(ListTest9, PopFrontEmptyThrows) { + List list; + EXPECT_THROW(list.pop_front(), const char*); +} -TEST(TestList4, can_delete_when_empty) { - - List list; - - ASSERT_ANY_THROW(list.pop_front()); +TEST(ListTest10, PopBackEmptyThrows) { + List list; + EXPECT_THROW(list.pop_back(), const char*); +} +TEST(ListTest11, FrontEmptyThrows) { + List list; + EXPECT_THROW(list.front(), const char*); +} +TEST(ListTest12, BackEmptyThrows) { + List list; + EXPECT_THROW(list.back(), const char*); } -TEST(TestList5, can_delete_when_empty) { +TEST(ListTest13, FindExisting) { + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); - List list; + Node* found = list.find(2); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->value, 2); +} - ASSERT_ANY_THROW(list.pop_back()); +TEST(ListTest14, FindNonExisting) { + List list; + list.push_back(1); + list.push_back(2); + Node* found = list.find(3); + EXPECT_EQ(found, nullptr); +} +TEST(ListTest15, FindInEmpty) { + List list; + Node* found = list.find(1); + EXPECT_EQ(found, nullptr); } +TEST(ListTest16, InsertAtFront) { + List list; + list.push_back(2); + list.insert(0, 1); + EXPECT_EQ(list.size(), 2); + EXPECT_EQ(list.front(), 1); + EXPECT_EQ(list.back(), 2); +} diff --git a/tests/tests_stack.cpp b/tests/tests_stack.cpp new file mode 100644 index 00000000..50852f74 --- /dev/null +++ b/tests/tests_stack.cpp @@ -0,0 +1,127 @@ +#include +#include "Stack.h" + + +TEST(StackTest, ConstructorWithSize) { + Stack stack(5); + EXPECT_TRUE(stack.is_empty()); + EXPECT_FALSE(stack.is_full()); +} + +TEST(StackTest, DefaultConstructor) { + Stack stack; + EXPECT_TRUE(stack.is_empty()); + EXPECT_TRUE(stack.is_full()); +} + +TEST(StackTest, PushElements) { + Stack stack(3); + + stack.push(10); + EXPECT_FALSE(stack.is_empty()); + EXPECT_EQ(stack.top(), 10); + + stack.push(20); + EXPECT_EQ(stack.top(), 20); + + stack.push(30); + EXPECT_TRUE(stack.is_full()); + EXPECT_EQ(stack.top(), 30); +} + +TEST(StackTest, PopElements) { + Stack stack(3); + stack.push(10); + stack.push(20); + stack.push(30); + + EXPECT_EQ(stack.top(), 30); + stack.pop(); + + EXPECT_EQ(stack.top(), 20); + stack.pop(); + + EXPECT_EQ(stack.top(), 10); + stack.pop(); + + EXPECT_TRUE(stack.is_empty()); +} + +TEST(StackTest, ClearStack) { + Stack stack(3); + stack.push(10); + stack.push(20); + + EXPECT_FALSE(stack.is_empty()); + stack.clear(); + EXPECT_TRUE(stack.is_empty()); +} + +TEST(StackTest, DifferentDataTypes) { + Stack doubleStack(3); + doubleStack.push(3.14); + doubleStack.push(2.71); + EXPECT_DOUBLE_EQ(doubleStack.top(), 2.71); + + Stack stringStack(2); + stringStack.push("Hello"); + stringStack.push("World"); + EXPECT_EQ(stringStack.top(), "World"); +} + +TEST(StackTest, SequenceOfOperations) { + Stack stack(5); + + for (int i = 1; i <= 5; ++i) { + stack.push(i * 10); + } + + EXPECT_TRUE(stack.is_full()); + EXPECT_EQ(stack.top(), 50); + + stack.pop(); + stack.pop(); + + EXPECT_EQ(stack.top(), 30); + EXPECT_FALSE(stack.is_empty()); + EXPECT_FALSE(stack.is_full()); + + stack.push(60); + stack.push(70); + + EXPECT_EQ(stack.top(), 70); + EXPECT_TRUE(stack.is_full()); + + stack.clear(); + EXPECT_TRUE(stack.is_empty()); + + stack.push(100); + EXPECT_EQ(stack.top(), 100); +} + +TEST(StackTest, IsEmptyMethod) { + Stack stack(2); + + EXPECT_TRUE(stack.is_empty()); + + stack.push(1); + EXPECT_FALSE(stack.is_empty()); + + stack.pop(); + EXPECT_TRUE(stack.is_empty()); +} + +TEST(StackTest, IsFullMethod) { + Stack stack(3); + + EXPECT_FALSE(stack.is_full()); + + stack.push(1); + stack.push(2); + stack.push(3); + + EXPECT_TRUE(stack.is_full()); + + stack.pop(); + EXPECT_FALSE(stack.is_full()); +} \ No newline at end of file From 43d4a794c1064f53ffa7cbe229716ecbee1f1c92 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Wed, 3 Dec 2025 12:17:46 +0300 Subject: [PATCH 13/13] //// --- lib_stack/Stack.h | 8 ++++---- tests/tests_stack.cpp | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib_stack/Stack.h b/lib_stack/Stack.h index 28708064..a9747a09 100644 --- a/lib_stack/Stack.h +++ b/lib_stack/Stack.h @@ -85,15 +85,15 @@ class StackList { StackList() = default; void push(const T& val) { - _list.push_back(val); + _list.push_front(val); } T pop() { if (_list.is_empty()) { throw std::logic_error("Stack is empty"); } - T value = _list.back(); - _list.pop_back(); + T value = _list.front(); + _list.pop_front(); return value; } @@ -101,7 +101,7 @@ class StackList { if (_list.is_empty()) { throw std::logic_error("Stack is empty"); } - return _list.back(); + return _list.front(); } bool is_empty() const { diff --git a/tests/tests_stack.cpp b/tests/tests_stack.cpp index 50852f74..abbde70b 100644 --- a/tests/tests_stack.cpp +++ b/tests/tests_stack.cpp @@ -27,6 +27,7 @@ TEST(StackTest, PushElements) { stack.push(30); EXPECT_TRUE(stack.is_full()); EXPECT_EQ(stack.top(), 30); + ASSERT_ANY_THROW(stack.push(40)); } TEST(StackTest, PopElements) {