-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLQueue.hpp
154 lines (115 loc) · 2.55 KB
/
LLQueue.hpp
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
149
150
151
152
153
154
#ifndef __PROJ2_QUEUE_HPP
#define __PROJ2_QUEUE_HPP
#include "runtimeexcept.hpp"
class QueueEmptyException : public RuntimeException
{
public:
QueueEmptyException(const std::string & err) : RuntimeException(err) {}
};
template<typename Object>
class LLQueue
{
private:
struct Node {
Node(Object v, Node * n) : val(v), next(n) {};
Object val;
Node * next;
};
Node * head = nullptr;
Node * rear = nullptr;
int numElements = 0;
public:
LLQueue(){};
// In principle, you should have copy constructors and
// assignment operators implemented.
// I am not going to require it for this assignment.
~LLQueue()
{
while (head != nullptr) {
Node * other = head;
head = head -> next;
delete other;
}
};
int size() const noexcept;
bool isEmpty() const noexcept;
Object & front();
const Object & front() const;
void enqueue(const Object & elem);
// does not return anything. Just removes.
void dequeue();
void printQueue();
LLQueue(const LLQueue & st) {
Node * tmp = st.head;
while (tmp != nullptr) {
enqueue(tmp->val);
tmp = tmp -> next;
}
};
LLQueue & operator=(const LLQueue & st) {
Node * tmp = st.head;
while (tmp != nullptr) {
enqueue(tmp->val);
tmp = tmp -> next;
}
return *this;
};
};
template<typename Object>
int LLQueue<Object>::size() const noexcept{
return numElements;
}
template<typename Object>
bool LLQueue<Object>::isEmpty() const noexcept{
return numElements == 0;
}
template<typename Object>
Object & LLQueue<Object>::front() {
if (head == nullptr) throw QueueEmptyException("No front of queue");
return head->val;
}
template<typename Object>
const Object & LLQueue<Object>::front() const {
if (head == nullptr) throw QueueEmptyException("No front of queue");
return head->val;
}
template<typename Object>
void LLQueue<Object>::enqueue(const Object & elem) {
Node* newNode = new Node(elem, nullptr);
if (head == nullptr) {
head = newNode;
}
else {
Node * tmp = head;
while (tmp -> next != nullptr) {
tmp = tmp -> next;
}
tmp -> next = newNode;
}
rear = newNode;
numElements++;
}
template<typename Object>
void LLQueue<Object>::dequeue() {
if (head == nullptr) throw QueueEmptyException("Cannot dequeue empty queue");
else {
Node * tmp = head;
head = head -> next;
if (head == nullptr) {
rear = nullptr;
}
delete tmp;
numElements--;
}
}
template<typename Object>
void LLQueue<Object>::printQueue() {
Node * tmp = head;
while (tmp != nullptr) {
Object o = (tmp -> val);
std::cout << o << " ";
tmp = tmp -> next;
}
std::cout << std::endl;
}
#endif