1 #pragma once
2
3 typedef int Type;
4
5 template <class Type> class SQueue{
6 private:
7 int front;
8 int rear;
9 int size;
10 Type *data;
11
12 bool IsFull();
13 bool IsEmpty();
14
15 public:
16 SQueue(int size);
17 ~SQueue();
18 bool EnSQueue(Type);
19 bool DeSQueue(Type *);
20 int GetLength();
21 };
1 #include <stdlib.h>
2 #include "SQueue.h"
3
4 template <class Type> SQueue<Type>::SQueue(int len)
5 {
6 data = new Type[len];
7 if (NULL == data)
8 exit(-1);
9
10 rear = 0;
11 front = 0;
12 size = len;
13 }
14
15 template <class Type> SQueue<Type> :: ~SQueue()
16 {
17 delete []data;
18 size = 0;
19 }
20
21 template <class Type> bool SQueue<Type> :: IsEmpty()
22 {
23 return rear == front;
24 }
25
26 template <class Type> bool SQueue<Type> :: IsFull()
27 {
28 return (rear + 1) % size == front;
29 }
30
31 template <class Type> bool SQueue<Type> :: EnSQueue(Type d)
32 {
33 if (IsFull())
34 return false;
35
36 data[rear] = d;
37 rear = (rear + 1) % size;
38
39 return true;
40 }
41
42 template <class Type> bool SQueue<Type> :: DeSQueue(Type *d)
43 {
44 if (IsEmpty())
45 return false;
46
47 *d = data[front];
48 front = (front + 1) % size;
49
50 return true;
51 }
52
53 template <class Type> int SQueue<Type> :: GetLength()
54 {
55 return (rear - front + size) % size;
56 }
1 #include <iostream>
2 #include "SQueue.h"
3 #include "SQueue.cpp"
4
5 using namespace std;
6
7 int main(int argc, char **argv)
8 {
9 int i = 0;
10 Type d;
11 Type a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
12 SQueue<Type> q(11);
13
14 while(i < 10)
15 q.EnSQueue(a[i++]);
16
17 while(q.DeSQueue(&d))
18 cout << "data = " << d << " " << "length = " << q.GetLength() << endl;
19
20 return 0;
21 }