-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomQueue.hpp
More file actions
148 lines (136 loc) · 2.51 KB
/
CustomQueue.hpp
File metadata and controls
148 lines (136 loc) · 2.51 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <functional>
#include "CustomNode.hpp"
typedef unsigned long long size_t;
template <class Value>
class CustomQueue
{
protected:
QueueNode<Value> *_top;
QueueNode<Value> *_last;
size_t _size;
// recursive deletion method
void recDestroy(QueueNode<Value> node)
{
if (node->next != nullptr)
recDestroy(node->next);
delete node;
}
public:
// constructor
CustomQueue()
{
this->_top = nullptr;
this->_last = nullptr;
this->_size = 0;
}
CustomQueue(Value value)
{
this->_top = new QueueNode<Value>(value);
this->_last = this->_top;
this->_size = 1;
}
CustomQueue(Value *start, Value *end)
{
this->_top = new QueueNode<Value>(*start);
this->_last = this->_top;
start++;
this->_size = 1;
while (start != end)
{
this->_last->next = new QueueNode<Value>(*start);
this->_last = this->_last->next;
start++;
this->_size++;
}
}
// destructor
~CustomQueue()
{
// iterative approach
QueueNode<Value> *current = this->_top;
QueueNode<Value> *next;
while (current != nullptr)
{
next = current->next;
delete current;
current = next;
}
this->_top = nullptr;
this->_last = nullptr;
this->_size = 0;
// recursive approah
// recDestroy(this->top);
}
// returns a constant reference to the top value
const Value &front() const
{
return this->_top->value;
}
const Value &back() const
{
return this->_last->value;
}
void push(const Value& value)
{
this->_last->next = new QueueNode<Value>(value);
this->_last = this->_last->next;
this->_size++;
}
void pop()
{
if (this->_top == nullptr)
return; // the queue is empty
QueueNode<Value> *newTop = this->_top->next;
delete this->_top;
this->_top = newTop;
this->_size--;
}
void empty()
{
~CustomQueue();
}
bool isempty()
{
return this->_size == 0;
}
size_t size()
{
return this->_size;
}
};
template <class Value, class Comparator = std::less<Value>>
class PriorityQueue : public CustomQueue<Value>
{
public:
PriorityQueue()
{
this->_top = nullptr;
this->_last = nullptr;
this->_size = 0;
}
PriorityQueue(const Value &value)
{
this->_top = new QueueNode<Value>(value);
this->_last = this->_top;
this->_size = 1;
}
~PriorityQueue()
{
// iterative approach
QueueNode<Value> *current = this->_top;
QueueNode<Value> *next;
while (current != nullptr)
{
next = current->next;
delete current;
current = next;
}
this->_top = nullptr;
this->_last = nullptr;
this->_size = 0;
}
// push method override
void push(const Value &value)
{
}
};