forked from georg-koch/Arduino-QueueList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueList.h
233 lines (191 loc) · 6.04 KB
/
QueueList.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* QueueList.h
*
* Library implementing a generic, dynamic queue (linked list version).
*
* ---
*
* Copyright (C) 2010 Efstathios Chatzikyriakidis (contact@efxa.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* Version 1.0
*
* 2010-09-28 Efstathios Chatzikyriakidis <contact@efxa.org>
*
* - added exit(), blink(): error reporting and handling methods.
*
* 2010-09-25 Alexander Brevig <alexanderbrevig@gmail.com>
*
* - added setPrinter(): indirectly reference a Serial object.
*
* 2010-09-20 Efstathios Chatzikyriakidis <contact@efxa.org>
*
* - initial release of the library.
*
* ---
*
* For the latest version see: http://www.arduino.cc/
*/
// header defining the interface of the source.
#ifndef _QUEUELIST_H
#define _QUEUELIST_H
// include Arduino basic header.
#include <Arduino.h>
// the definition of the queue class.
template<typename T>
class QueueList {
public:
// init the queue (constructor).
QueueList ();
// clear the queue (destructor).
~QueueList ();
// push an item to the queue.
void push (const T i);
// pop an item from the queue.
T pop ();
// get an item from the queue.
T peek () const;
// check if the queue is empty.
bool isEmpty () const;
// get the number of items in the queue.
int count () const;
// set the printer of the queue.
void setPrinter (Print & p);
private:
// exit report method in case of error.
void exit (const char * m) const;
// led blinking method in case of error.
void blink () const;
// the pin number of the on-board led.
static const int ledPin = 13;
// the structure of each node in the list.
typedef struct node {
T item; // the item in the node.
node * next; // the next node in the list.
} node;
typedef node * link; // synonym for pointer to a node.
Print * printer; // the printer of the queue.
int size; // the size of the queue.
link head; // the head of the list.
link tail; // the tail of the list.
};
// init the queue (constructor).
template<typename T>
QueueList<T>::QueueList () {
size = 0; // set the size of queue to zero.
head = NULL; // set the head of the list to point nowhere.
tail = NULL; // set the tail of the list to point nowhere.
printer = NULL; // set the printer of queue to point nowhere.
}
// clear the queue (destructor).
template<typename T>
QueueList<T>::~QueueList () {
// deallocate memory space of each node in the list.
for (link t = head; t != NULL; head = t) {
t = head->next; delete head;
}
size = 0; // set the size of queue to zero.
tail = NULL; // set the tail of the list to point nowhere.
printer = NULL; // set the printer of queue to point nowhere.
}
// push an item to the queue.
template<typename T>
void QueueList<T>::push (const T i) {
// create a temporary pointer to tail.
link t = tail;
// create a new node for the tail.
tail = (link) new node;
// if there is a memory allocation error.
if (tail == NULL)
exit ("QUEUE: insufficient memory to create a new node.");
// set the next of the new node.
tail->next = NULL;
// store the item to the new node.
tail->item = i;
// check if the queue is empty.
if (isEmpty ())
// make the new node the head of the list.
head = tail;
else
// make the new node the tail of the list.
t->next = tail;
// increase the items.
size++;
}
// pop an item from the queue.
template<typename T>
T QueueList<T>::pop () {
// check if the queue is empty.
if (isEmpty ())
exit ("QUEUE: can't pop item from queue: queue is empty.");
// get the item of the head node.
T item = head->item;
// remove only the head node.
link t = head->next; delete head; head = t;
// decrease the items.
size--;
// return the item.
return item;
}
// get an item from the queue.
template<typename T>
T QueueList<T>::peek () const {
// check if the queue is empty.
if (isEmpty ())
exit ("QUEUE: can't peek item from queue: queue is empty.");
// return the item of the head node.
return head->item;
}
// check if the queue is empty.
template<typename T>
bool QueueList<T>::isEmpty () const {
return head == NULL;
}
// get the number of items in the queue.
template<typename T>
int QueueList<T>::count () const {
return size;
}
// set the printer of the queue.
template<typename T>
void QueueList<T>::setPrinter (Print & p) {
printer = &p;
}
// exit report method in case of error.
template<typename T>
void QueueList<T>::exit (const char * m) const {
// print the message if there is a printer.
if (printer)
printer->println (m);
// loop blinking until hardware reset.
blink ();
}
// led blinking method in case of error.
template<typename T>
void QueueList<T>::blink () const {
// set led pin as output.
pinMode (ledPin, OUTPUT);
// continue looping until hardware reset.
while (true) {
digitalWrite (ledPin, HIGH); // sets the LED on.
delay (250); // pauses 1/4 of second.
digitalWrite (ledPin, LOW); // sets the LED off.
delay (250); // pauses 1/4 of second.
}
// solution selected due to lack of exit() and assert().
}
#endif // _QUEUELIST_H