首页 > 代码库 > Queue1

Queue1

 1 #include<iostream>
 2 using namespace std;
 3 //#define maxSize 20
 4 template <class T>
 5 class Queue
 6 {
 7 private :
 8 //    T data[maxSize];
 9     int maxSize;
10     T *data;
11     T Front;
12     T rear;
13     int Count;
14 public:
15     Queue()
16     {
17         Front = 0;
18         rear = 0;
19         Count = 0;
20         cout<<"input the queue maxsize"<<endl;
21         cin>>maxSize;
22         data = http://www.mamicode.com/new T[maxSize];
23     }
24     void enQueue( T & x)
25     {
26         if(Count > 0 && Front == rear)
27         {
28             cout<<"Queue is full"<<endl;
29         }
30         data[rear] = x;
31         rear = (rear + 1) % maxSize;
32         Count++;
33     }
34     void outQueue()
35     {
36         if(Count == 0)
37         {
38             cout <<"Queue is empty"<<endl;
39         }
40         T tem = data[Front];
41         Front = (Front + 1 )% maxSize;
42         cout<<"out elem:"<<tem<<endl;
43     }
44     int isEmpty()
45     {
46         return Count == 0;
47     }
48     void makeEmpty()
49     {
50         Front = 0;
51         rear = 0;
52         Count = 0;
53     }
54     T readhead()
55     {
56         T tem = data[Front];
57         return tem;
58     }
59     void printQueue()
60     {
61         int j = Front;
62         for(int i = 0 ; i < Count ; i++ )
63         {
64             cout<<data[j]<<" ";
65             j = ( j + 1 )%maxSize;
66         }
67     }
68     int getlength()
69     {
70         return Count;
71     }
72 };
73 int main()
74 {
75     Queue <int> dusk;
76     int j;
77     cin>>j;
78     for(int i = 0 ; i < j ; i++ )
79     {
80         dusk.enQueue(i);
81     }
82     dusk.printQueue();
83 
84 }