今天实现了一个优先级队列,经过调试,模版实现必须和声明在同一个文件中:
1 template
2 class Priqueque
3 {
4 private:
5 int maxSize;
6 int n;
7 T* queque;
8 void Swap(int i,int j)
9 {
10 T temp = queque[i];
11 queque[i] = queque[j];
12 queque[j] = temp;
13 }
14 public:
15 Priqueque(int maxSize);//³õʼ»¯ÓÅÏȼ¶¶ÓÁÐ
16 bool insert(T t);
17 T extractMin();
18
19 };
20 template Priqueque::Priqueque(int maxSize)
21 {
22 queque = new T[maxSize + 1];
23 this->maxSize = maxSize;
24 n = 0;
25 }
26 template bool Priqueque::insert(T t)
27 {
28 if (n >= maxSize)
29 {
30 return false;
31 }
32 n++;
33 queque[n] = t;
34 for (int i = n,p = i/2;i > 1 && queque[i] < queque[p];i = p,p = i/2)
35 {
36 Swap(i,p);
37 }
38 return true;
39 }
40 template T Priqueque::extractMin()
41 {
42 T ret_value = queque[1];
43 queque[1] = queque[n];
44 n--;
45 int i = 1;
46 int c = 2 * i;
47 while(c |