-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue.h
More file actions
61 lines (55 loc) · 798 Bytes
/
queue.h
File metadata and controls
61 lines (55 loc) · 798 Bytes
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
#include<assert.h>
template<class T>
class queue
{
struct listnode {
listnode* next;
T value;
listnode(listnode* node, T v): next(node), value(v) {}
};
public:
queue(): begin(nullptr), end(nullptr), size(0) {}
~queue()
{
while (begin != nullptr)
{
listnode* temp = begin;
begin = begin->next;
delete temp;
}
}
void enqueue(const T& value)
{
if (size == 0)
{
begin = new listnode(nullptr, value);
end = begin;
}
else
{
end->next = new listnode(nullptr, value);
end = end->next;
}
size++;
}
void dequeue()
{
assert(size > 0);
listnode* temp = begin;
begin = begin->next;
delete temp;
size--;
}
T front()
{
return begin->value;
}
int length()
{
return size;
}
private:
listnode* begin;
listnode* end;
int size;
};