From 977545c1eeb510d783d147df3ace44f01a7373ab Mon Sep 17 00:00:00 2001 From: liza-leonova Date: Tue, 24 Dec 2024 00:29:49 +0500 Subject: [PATCH] Update circular_queue.cpp --- task3/circular_queue.cpp | 53 ++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index fa74734..9cc492d 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,36 +1,47 @@ #include "circular_queue.hpp" +#include -CircularQueue::CircularQueue(size_t size) -{ - // your implementation here +CircularQueue::CircularQueue(size_t max_size) : max_capacity(max_size), current_size(0), head_index(0), tail_index(0) { + storage.resize(max_size); // Pre-allocate exact size } -bool CircularQueue::Push(int value) -{ - // your implementation here +bool CircularQueue::Push(int value) { + if (IsFull()) { + return false; + } + storage[tail_index] = value; + tail_index = (tail_index + 1) % max_capacity; + current_size++; + return true; } -bool CircularQueue::Pop() -{ - // your implementation here +bool CircularQueue::Pop() { + if (IsEmpty()) { + return false; + } + head_index = (head_index + 1) % max_capacity; + current_size--; + return true; } -int CircularQueue::Front() const -{ - // your implementation here +int CircularQueue::Front() const { + if (IsEmpty()) { + return -1; + } + return storage[head_index]; } -int CircularQueue::Back() const -{ - // your implementation here +int CircularQueue::Back() const { + if (IsEmpty()) { + return -1; + } + return storage[(tail_index - 1 + max_capacity) % max_capacity]; } -bool CircularQueue::Empty() const -{ - // your implementation here +bool CircularQueue::IsEmpty() const { + return current_size == 0; } -bool CircularQueue::Full() const -{ - // your implementation here +bool CircularQueue::IsFull() const { + return current_size == max_capacity; }