1
//數(shù)據(jù)結(jié)構(gòu)隊列簡單實現(xiàn)(循環(huán)隊列)
2
#ifndef QUEUE_H
3
#define QUEUE_H
4
5
template<class T>
6
class Queue
7

{
8
public:
9
Queue(int maxsize=50);
10
virtual ~Queue();
11
bool IsFull()
{return (rear+1)%MaxSize==front?true:false;}//滿?
12
bool IsEmpty()
{return front==rear?true:false;};//空?
13
Queue<T>& Push(const T& val);//向隊尾插入元素
14
Queue<T>& Pop(T& e);//從堆頭刪除元素
15
int Size()
{return _Size;};//返回隊列元素個數(shù)
16
T& Front();//返回對頭元素
17
T& Back();//返回隊尾元素
18
19
private:
20
T *data;
21
int front,rear;
22
int MaxSize;
23
int _Size;
24
};
25
//------------------------------------------------
26
template<class T>
27
Queue<T>::Queue(int maxsize)
28

{
29
data=new T[maxsize+1];
30
MaxSize=maxsize+1;
31
front=rear=_Size=0;
32
33
}
34
//------------------------------------------------
35
template<class T>
36
Queue<T>::~Queue()
37

{
38
delete[] data;
39
}
40
//------------------------------------------------
41
template<class T>
42
Queue<T>& Queue<T>::Push(const T& val)
43

{
44
if(IsFull()) throw exception("隊列已滿");
45
rear=(rear+1)%MaxSize;
46
data[rear]=val;
47
_Size++;
48
return *this;
49
}
50
//------------------------------------------------
51
template<class T>
52
Queue<T>& Queue<T>::Pop(T& e)
53

{
54
if(IsEmpty()) throw exception("隊列已空");
55
front=(front+1)%MaxSize;
56
e=data[front];
57
_Size--;
58
return *this;
59
}
60
//------------------------------------------------
61
template<class T>
62
T& Queue<T>::Front()
63

{
64
if(IsEmpty()) throw exception("隊列已空");
65
return data[(front+1)%MaxSize];
66
67
}
68
//------------------------------------------------
69
template<class T>
70
T& Queue<T>::Back()
71

{
72
if(IsEmpty()) throw exception("隊列已空");
73
return data[rear];
74
75
}
76
#endif
//數(shù)據(jù)結(jié)構(gòu)隊列簡單實現(xiàn)(循環(huán)隊列)2
#ifndef QUEUE_H3
#define QUEUE_H4

5
template<class T>6
class Queue7


{8
public:9
Queue(int maxsize=50); 10
virtual ~Queue();11

bool IsFull()
{return (rear+1)%MaxSize==front?true:false;}//滿?12

bool IsEmpty()
{return front==rear?true:false;};//空?13
Queue<T>& Push(const T& val);//向隊尾插入元素14
Queue<T>& Pop(T& e);//從堆頭刪除元素15

int Size()
{return _Size;};//返回隊列元素個數(shù)16
T& Front();//返回對頭元素17
T& Back();//返回隊尾元素18
19
private:20
T *data;21
int front,rear;22
int MaxSize;23
int _Size;24
};25
//------------------------------------------------26
template<class T>27
Queue<T>::Queue(int maxsize)28


{29
data=new T[maxsize+1];30
MaxSize=maxsize+1;31
front=rear=_Size=0;32
33
}34
//------------------------------------------------35
template<class T>36
Queue<T>::~Queue()37


{38
delete[] data;39
}40
//------------------------------------------------41
template<class T>42
Queue<T>& Queue<T>::Push(const T& val)43


{44
if(IsFull()) throw exception("隊列已滿");45
rear=(rear+1)%MaxSize;46
data[rear]=val;47
_Size++;48
return *this;49
}50
//------------------------------------------------51
template<class T>52
Queue<T>& Queue<T>::Pop(T& e)53


{54
if(IsEmpty()) throw exception("隊列已空");55
front=(front+1)%MaxSize;56
e=data[front];57
_Size--;58
return *this;59
}60
//------------------------------------------------61
template<class T>62
T& Queue<T>::Front()63


{64
if(IsEmpty()) throw exception("隊列已空");65
return data[(front+1)%MaxSize];66
67
}68
//------------------------------------------------69
template<class T>70
T& Queue<T>::Back()71


{72
if(IsEmpty()) throw exception("隊列已空");73
return data[rear];74
75
}76
#endif

