-
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
1 parent
4975222
commit 1220ec0
Showing
1 changed file
with
32 additions
and
22 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 <vector> | ||
|
||
CircularQueue::CircularQueue(size_t size) | ||
{ | ||
// your implementation here | ||
CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0), size(0) { | ||
buffer.resize(capacity); | ||
} | ||
|
||
bool CircularQueue::Push(int value) | ||
{ | ||
// your implementation here | ||
bool CircularQueue::Push(int value) { | ||
if (IsFull()) { | ||
return false; // Очередь полна | ||
} | ||
buffer[rear] = value; // Добавляем элемент в очередь | ||
rear = (rear + 1) % capacity; // Обновляем заднюю границу | ||
++size; // Увеличиваем размер очереди | ||
return true; | ||
} | ||
|
||
bool CircularQueue::Pop() | ||
{ | ||
// your implementation here | ||
bool CircularQueue::Pop() { | ||
if (IsEmpty()) { | ||
return false; // Очередь пуста | ||
} | ||
front = (front + 1) % capacity; // Удаляем элемент из очереди | ||
--size; // Уменьшаем размер очереди | ||
return true; | ||
} | ||
|
||
int CircularQueue::Front() const | ||
{ | ||
// your implementation here | ||
int CircularQueue::Front() const { | ||
if (IsEmpty()) { | ||
return -1; // Если очередь пуста, возвращаем -1 | ||
} | ||
return buffer[front]; // Возвращаем элемент на передней границе | ||
} | ||
|
||
int CircularQueue::Back() const | ||
{ | ||
// your implementation here | ||
int CircularQueue::Back() const { | ||
if (IsEmpty()) { | ||
return -1; // Если очередь пуста, возвращаем -1 | ||
} | ||
return buffer[(rear + capacity - 1) % capacity]; // Возвращаем последний элемент | ||
} | ||
|
||
bool CircularQueue::Empty() const | ||
{ | ||
// your implementation here | ||
bool CircularQueue::IsEmpty() const { | ||
return size == 0; // Проверка на пустоту | ||
} | ||
|
||
bool CircularQueue::Full() const | ||
{ | ||
// your implementation here | ||
} | ||
bool CircularQueue::IsFull() const { | ||
return size == capacity; // Проверка на полноту |