From d90700be9542df0f58ab938461736c6adca88f45 Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Tue, 4 Nov 2025 15:12:27 +0300 Subject: [PATCH 1/2] 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 ed3c9ec7ce73ba9594f2a78027196e9cdbdfc1ac Mon Sep 17 00:00:00 2001 From: SisAdmin-byte Date: Wed, 3 Dec 2025 10:48:06 +0300 Subject: [PATCH 2/2] Update queue.h --- lib_queue/queue.h | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index 9611de36..0cc4fbd3 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -25,7 +25,6 @@ class Queue { int capacity() const; void clear(); int tail() const; - void resize(int newCapacity); }; @@ -159,15 +158,3 @@ 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