看看这个程序,不知道写得对不对?

Areslp 2004-02-14 01:53:57
利用书中图5.19和图5.20的最大值堆实现一个优先队列。对于队列的操作应该支持下列几种指令:
void enqueue(int ObjectID, int Priority);
int dequeue();
void changeweight(int ObjectID, int newPriority);
函数enqueue向优先队列中插入一个ID号为ObjectID、优先级为Priority的新对象。函数dequeue从优先队列中删除优先级最高的对象,并返回该对象的ID号。函数changeweight将ID号为ObjectID的对象的优先级改为newPriority。类型Elem应该是一个存储对象ID及其优先级的类。你需要一种机制,以便获取所需对象在堆中的位置。利用一个数组,将ObjectID值为i的对象存放在数组位置i处(记住测试时应该保证ObjectID的数值在数组的边界限定之内)。你还需要对堆的实现进行修改,以存储对象在数组中的位置,使得堆中对象的修改可以在辅助数组结构中记录下来。

上面是题目,我写得程序如下:

#include <iostream>
#include <string>
#define MAX 100
using namespace std;
class Elem{
public:
int id;
int pri;
};
class Comp{
public:
static bool It(Elem x,Elem y){
return x.pri<y.pri;
}
static bool eq(Elem x,Elem y){
return x.pri==y.pri;
}
static bool gt(Elem x,Elem y){
return x.pri>y.pri;
}
};
template <class Elem,class Comp> class maxheap{
Elem* Heap;
int size;
int n;
char bk[MAX];
void siftdown(int);
public:
void enqueue(int id,int pri);
int dequeue();
void changeweight(int id,int pri);
maxheap(Elem* h,int num,int max)
{
Heap=h;
n=num;
size=max;
buildHeap();
}
int heapsize() const
{
return n;
}
bool isLeaf(int pos) const
{
return (pos>=n/2)&&(pos<n);
}
int leftchild(int pos) const
{
return 2*pos+1;
}
int rightchild(int pos) const
{
return 2*pos+2;
}
int parent(int pos) const
{
return (pos-1)/2;
}
bool insert(const Elem&);
bool removemax(Elem&);
bool remove(int,Elem&);
void buildHeap()
{
for(int i=n/2-1;i>=0;i--)
siftdown(i);
}
};
template <class Elem,class Comp>
void maxheap<Elem,Comp>::siftdown(int pos){
while(!isLeaf(pos){
int j=leftchild(pos);
int rc=rightchild(pos);
if((rc<n)&&Comp::It(Heap[j],Heap[rc])))
j=rc;
if(!Comp::It(Heap[pos],Head[j]))
swap(Heap,pos,j);
pos=j;
}
}
template <class Elem,class Comp>
bool maxheap<Elem,Comp>::insert(const Elem& val){
if(n>=size) return false;
int curr=n++;
Heap[curr]=val;
while((curr!=0)&&(Comp::gt(Heap[curr],Heap[parent(curr)]))){
swap(Heap,curr,parent(curr));
curr=parent(curr);
}
bk[val.id-1]=val.id;
return true;
}
template <class Elem,class Comp>
bool maxheap<Elem,Comp>::removemax(Elem& it){
if(n==0)
return false;
swap(Heap,0,--n);
if(n!=0)
siftdown(0);
it=Heap[n];
return true;
}
template <class Elem,class Comp>
bool maxheap<Elem,Comp>::remove(int pos,Elem& it){
if((pos<0)||(pos>=n))
return false;
swap(Heap,pos,--n);
while((pos!=0)&&(Comp::gt(Heap[pos],Heap[parent(pos)])))
swap(Heap,pos,parent(pos));
siftdown(pos);
it=Heap[n];
return true;
}
template <class Elem,class Comp>
void enqueue(int id,int pri){
Elem* p;
p->id=id;
p->pri=pri;
insert(*p);
}
template <class Elem,class Comp>
int dequeue()
{
int max=0;
for(int i=0;i<n;i++)
if(Heap[i].pri<Heap[i+1].pri)
max=i+1;
removemax(Heap[max]);
bk[max-1]=NULL;
return Heap[max].id;
}
template <class Elem,class Comp>
void changeweight(int id,int pri)
{
for(int i=0;i<n;i++)
if(Heap[i].id==id)
Heap[i].pri=pri;
}
void main()
{
}
不知道写得对不对
就是函数enqueue和函数dequeue我觉得可以用书上写得那个template <class Elem,class Comp>
bool maxheap<Elem,Comp>::insert(const Elem& val)和
bool removemax(Elem&);来实现,我不确定这样写行不行,帮忙看看吧,谢谢谢谢!!!
...全文
25 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧