-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b08db55
commit 977545c
Showing
1 changed file
with
32 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |