Skip to content

Commit

Permalink
Update circular_queue.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
liza-leonova authored Dec 23, 2024
1 parent b08db55 commit 977545c
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions task3/circular_queue.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
#include "circular_queue.hpp"
#include <vector>

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;
}

0 comments on commit 977545c

Please sign in to comment.