首页 > 代码库 > Queue2

Queue2

 1 #include <iostream>
 2 using namespace std;
 3 template <class T>
 4 class Queue
 5 {
 6 private:
 7     struct node
 8     {
 9         T data;
10         node * next;
11         node():next(NULL) {}
12         node(T d):data(d),next(NULL) {}
13     };
14     node * Front;
15     node * rear;
16     node * head;
17 
18 public:
19     int Count;
20     Queue()
21     {
22         node * tem = new node();
23         Front = tem;
24         rear  = tem;
25         head = tem;
26         Count = 0;
27     }
28     bool isEmpty()
29     {
30         return Count == 0;
31     }
32     void makeEmpty()
33     {
34         while(Front != NULL)
35         {
36             node * p = Front;
37             Front = Front -> next;
38             delete p;
39         }
40         Count = 0;
41     }
42     void enQueue(T d)
43     {
44         node * tem = new node(d);
45         rear -> next = tem;
46         rear = rear -> next;
47         Count++;
48     }
49     void printQueue()
50     {
51         node * cur = Front;
52         while(cur != NULL)
53         {
54             cout<<cur -> data<<" ";
55             cur = cur -> next;
56         }
57     }
58     void outQueue()
59     {
60         cout<<Front->data<<" ";
61         node * p = Front;
62         Front = Front -> next;
63         delete (p);
64     }
65     T getTop()
66     {
67         return Front->data;
68     }
69     void init()
70     {
71         Front = Front -> next;
72         rear = rear -> next;
73     }
74 };
75 int main()
76 {
77     Queue<int> dusk;
78 
79     for(int i = 0 ; i < 100 ; i++)
80     {
81         dusk.enQueue(i);
82     }
83     dusk.init();
84     dusk.printQueue();
85     cout<<endl;
86     cout<<dusk.getTop();
87 
88 }