Skip to content

Commit

Permalink
Updated circular_queue.cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy-github committed Dec 21, 2024
1 parent 0fa23dc commit 335586a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
54 changes: 47 additions & 7 deletions task3/circular_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,75 @@

CircularQueue::CircularQueue(size_t size)
{
// your implementation here
sz = size;
front = -1;
rear = -1;
}

bool CircularQueue::Push(int value)
{
// your implementation here
if (this->Full())
{
return false;
}
if (front == -1)
{
front = 0;
}
rear = (rear + 1) % sz;
q.push_back(value);
return true;
}

bool CircularQueue::Pop()
{
// your implementation here
if (this->Empty())
{
return false;
}
q.erase(q.begin() + front);
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front = (front + 1) % sz;
}
return true;
}

int CircularQueue::Front() const
{
// your implementation here
return q[front];
}

int CircularQueue::Back() const
{
// your implementation here
return q[rear];
}

bool CircularQueue::Empty() const
{
// your implementation here
if (front <= -1 || rear <= -1)
{
return true;
}
else
{
return false;
}
}

bool CircularQueue::Full() const
{
// your implementation here
if ((front == 0 && rear == sz - 1) || (front == (rear + 1) % sz))
{
return true;
}
else
{
return false;
}
}
4 changes: 4 additions & 0 deletions task3/circular_queue.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

#include <cstddef>
#include <vector>

class CircularQueue {
private:
std::vector<int> q;
int front, rear, sz;
public:
CircularQueue(size_t size); // создать очередь с определенным размером буффера
bool Push(int value); // добавить значение в конец очереди (false, если очередь заполнена)
Expand Down

0 comments on commit 335586a

Please sign in to comment.