forked from bitdog-io/restraining_bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
82 lines (69 loc) · 1.3 KB
/
Copy pathQueue.cpp
File metadata and controls
82 lines (69 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
//
//
#include "Queue.h"
#include <cstdlib>
// Constructor to initialize queue
Queue::Queue( int size )
{
_arr = new const char*[size];
_capacity = size;
_front = 0;
_rear = -1;
_count = 0;
}
// Destructor to free memory allocated to the queue
Queue::~Queue()
{
delete _arr;
}
// Utility function to remove front element from the queue
const char * Queue::dequeue()
{
const char* item;
// check for queue underflow
if ( isEmpty() )
{
Log.error( "Queue was empty when dequeue attempted" );
return nullptr;
}
item = _arr[_front];
_front = (_front + 1) % _capacity;
_count--;
return item;
}
// Utility function to add an item to the queue
void Queue::enqueue(const char* item )
{
// check for queue overflow
if ( isFull() )
{
Log.error( "Queue was full when enqueue attempted" );
}
_rear = (_rear + 1) % _capacity;
_arr[_rear] = item;
_count++;
}
// Utility function to return front element in the queue
const char* Queue::peek()
{
if ( isEmpty() )
{
}
return _arr[_front];
}
// Utility function to return the size of the queue
int Queue::size()
{
return _count;
}
// Utility function to check if the queue is empty or not
bool Queue::isEmpty()
{
return (size() == 0);
}
// Utility function to check if the queue is full or not
bool Queue::isFull()
{
return (size() >= _capacity);
}