From 335586a330fd43837bcfed2968a7c4be94f8fdbc Mon Sep 17 00:00:00 2001 From: Dimtron Date: Sat, 21 Dec 2024 18:32:20 +0500 Subject: [PATCH] Updated circular_queue.cpp. --- task3/circular_queue.cpp | 54 ++++++++++++++++++++++++++++++++++------ task3/circular_queue.hpp | 4 +++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index fa74734..8b86fe1 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -2,35 +2,75 @@ CircularQueue::CircularQueue(size_t size) { - // your implementation here + sz = size; + front = -1; + rear = -1; } bool CircularQueue::Push(int value) { - // your implementation here + if (this->Full()) + { + return false; + } + if (front == -1) + { + front = 0; + } + rear = (rear + 1) % sz; + q.push_back(value); + return true; } bool CircularQueue::Pop() { - // your implementation here + if (this->Empty()) + { + return false; + } + q.erase(q.begin() + front); + if (front == rear) + { + front = -1; + rear = -1; + } + else + { + front = (front + 1) % sz; + } + return true; } int CircularQueue::Front() const { - // your implementation here + return q[front]; } int CircularQueue::Back() const { - // your implementation here + return q[rear]; } bool CircularQueue::Empty() const { - // your implementation here + if (front <= -1 || rear <= -1) + { + return true; + } + else + { + return false; + } } bool CircularQueue::Full() const { - // your implementation here + if ((front == 0 && rear == sz - 1) || (front == (rear + 1) % sz)) + { + return true; + } + else + { + return false; + } } diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 2871091..2d08b83 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -1,8 +1,12 @@ #pragma once #include +#include class CircularQueue { +private: + std::vector q; + int front, rear, sz; public: CircularQueue(size_t size); // создать очередь с определенным размером буффера bool Push(int value); // добавить значение в конец очереди (false, если очередь заполнена)