📄 wex13_17.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
#pragma hdrstop
#include "bstree.h"
template <class T>
class PQueue
{
private:
// search tree that stores the queue list
BinSTree<T> pqlist;
T Min(void);
public:
// constructor
PQueue(void);
// priority queue modification operations
void PQInsert(const T& item);
T PQDelete(void);
void ClearPQ(void);
// priority queue test methods
int PQEmpty(void) const;
int PQLength(void) const;
};
template <class T>
T PQueue<T>::Min(void)
{
TreeNode<T> *t = pqlist.GetRoot();
while(t->Left() != NULL)
t = t->Left();
return t->data;
}
template <class T>
PQueue<T>::PQueue(void)
{}
template <class T>
void PQueue<T>::PQInsert(const T& item)
{
pqlist.Insert(item);
}
// delete the first element in the queue by deleting the
// minimum value from the binary search tree. return the deleted value
template <class T>
T PQueue<T>::PQDelete(void)
{
T minval;
if (pqlist.ListEmpty())
{
cerr << "PQDelete: list empty!" << endl;
exit(1);
}
minval = Min();
pqlist.Delete(minval);
return minval;
}
template <class T>
int PQueue<T>::PQEmpty(void) const
{
return pqlist.ListEmpty();
}
template <class T>
int PQueue<T>::PQLength(void) const
{
return pqlist.ListSize();
}
template <class T>
void PQueue<T>::ClearPQ(void)
{
pqlist.ClearList();
}
void main(void)
{
int a[] = {12,4,7,10,11,6,5,3,15,9,33,25};
PQueue<int> P;
for(int i=0;i < 12;i++)
P.PQInsert(a[i]);
while (!P.PQEmpty())
cout << P.PQDelete() << " ";
cout << endl;
}
/*
<Run>
3 4 5 6 7 9 10 11 12 15 25 33
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -