-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.h
65 lines (65 loc) · 891 Bytes
/
queue.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
#ifndef SJTU_QUEUE_HPP
#define SJTU_QUEUE_HPP
namespace stl
{
template<class T>
class queue
{
private:
struct node
{
T data;
node *next;
node(const T &x, node *N = NULL) : data(x), next(N) {}
~node() {}
};
node *first, *last;
int Size;
public:
queue()
{
first = last = NULL;
Size = 0;
}
~queue()
{
clear();
}
bool empty()
{
return first == NULL;
}
void clear()
{
node *tmp;
while (first != NULL) {
tmp = first;
first = first->next;
delete tmp;
}
Size = 0;
}
T front()
{
return first->data;
}
void push(const T &x)
{
if (last == NULL)
first = last = new node(x);
else
last = last->next = new node(x);
}
T pop()
{
node *tmp = first;
T value = first->data;
first = first->next;
if (first == NULL)
last = NULL;
delete tmp;
return value;
}
};
}
#endif