-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.h
72 lines (58 loc) · 1.19 KB
/
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
66
67
68
69
70
71
/*
* File: Queue.h
* Author: try
*
* Created on 2011年7月11日, 下午3:04
*/
#ifndef QUEUE_H
#define QUEUE_H
#include <stdlib.h>
#include <sys/types.h>
/**
* 一个简单的队列, 用法与stl库的queue差不多
*/
template<typename T>
class Queue {
public:
Queue():qsize(0), frontE(NULL),backE(NULL){
}
virtual ~Queue(){}
void push(T o){
if(frontE){
backE->next = new Entry(o);
backE = backE->next;
}else{
frontE = new Entry(o);
backE = frontE;
}
qsize++;
}
T front(){
return frontE->element;
}
void pop(){
if(frontE){
Entry* e = frontE;
frontE = frontE->next;
delete e;
qsize--;
}
}
bool empty(){
return qsize == 0;
}
size_t size(){
return qsize;
}
private:
typedef struct Entry{
T element;
Entry* next; //下一个元素
Entry(T e):element(e), next(NULL){}
}Entry;
private:
size_t qsize;
Entry* frontE; //前端元素
Entry* backE; //后端元素
};
#endif /* QUEUE_H */