-
Notifications
You must be signed in to change notification settings - Fork 33
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
Showing
1 changed file
with
31 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,46 @@ | ||
#include "circular_queue.hpp" | ||
#include <stdexcept> | ||
|
||
CircularQueue::CircularQueue(size_t size) | ||
{ | ||
// your implementation here | ||
} | ||
: buffer(size), head(0), tail(0), capacity(size), size(0) {} | ||
|
||
bool CircularQueue::Push(int value) | ||
{ | ||
// your implementation here | ||
bool CircularQueue::Push(int value) { | ||
if (Full()) { | ||
return false; | ||
} | ||
buffer[tail] = value; | ||
tail = (tail + 1) % capacity; | ||
size++; | ||
return true; | ||
} | ||
|
||
bool CircularQueue::Pop() | ||
{ | ||
// your implementation here | ||
bool CircularQueue::Pop() { | ||
if (Empty()) { | ||
return false; | ||
} | ||
head = (head + 1) % capacity; | ||
size--; | ||
return true; | ||
} | ||
|
||
int CircularQueue::Front() const | ||
{ | ||
// your implementation here | ||
int CircularQueue::Front() const { | ||
if (Empty()) { | ||
return -1; | ||
} | ||
return buffer[head]; | ||
} | ||
|
||
int CircularQueue::Back() const | ||
{ | ||
// your implementation here | ||
int CircularQueue::Back() const { | ||
if (Empty()) { | ||
return -1; | ||
} | ||
return buffer[(tail + capacity - 1) % capacity]; | ||
} | ||
|
||
bool CircularQueue::Empty() const | ||
{ | ||
// your implementation here | ||
bool CircularQueue::Empty() const { | ||
return size == 0; | ||
} | ||
|
||
bool CircularQueue::Full() const | ||
{ | ||
// your implementation here | ||
bool CircularQueue::Full() const { | ||
return size == capacity; | ||
} |