使用1维数组实现“优先级队列”,实现好该类后,自己编写主程序验证该类各方法正确性。
struct Queue_entry {
data_entry data;
int priority;
};
const int maxqueue = 50; // small value for testing
class PriorityQueue {
public:
PriorityQueue();
bool empty() const;
Error_code serve();
Error_code append(const Queue_entry &item);
Error_code retrieve(Queue_entry &item) const;
protected:
int rear; // front stays at 0
Queue_entry entry[maxqueue];
};
PriorityQueue:: PriorityQueue()
/* Post: The PriorityQueue is initialized to be empty. */
{ }
bool PriorityQueue::empty() const
/* Post: Return true if the PriorityQueue is empty, otherwise return false. */
{ }
Error_code PriorityQueue::serve()
/* Post: The front of the PriorityQueue is removed. If the PriorityQueue is empty return an Error_code of underflow. */
{ }
Error_code PriorityQueue::retrieve(Queue_entry &item) const
/* Post: The front of the PriorityQueue retrieved to the output parameter item. If the PriorityQueue is empty return an Error_code of underflow. */
{ }
Error_code PriorityQueue::append(const Queue_entry &item)
/* Post: item is added to the rear of the PriorityQueue and adjust to the proper position by priority field. If the PriorityQueue is full return an Error_code of overflow.*/
{ }
注意:
0号下标为队首元素;
优先级priority的数值越大表明越应放在靠近队首的地方;优先级最高的为队首元素。
实现提示:
插入一个元素时,先放在队尾,之后根据其优先级把它按照类似排序的方法向前移到合适的位置(其后元素优先级小于该元素优先级;其前元素优先级大于或等于该元素优先级)。因此,队列中元素实际上是按照优先级递减的。
删除一个元素时,出队的元素是队列首元(优先级最高的元素)。
数据结构2017级大作业01_测试用例
-------------------------------------------
=========================================
注意:此次检测的例子中,数据为字符:char
=========================================
========
PART 1
========
typedef char data_entry;
========
PART 2
========
int main()
{
cout << "Priority Queue!" << endl;
PriorityQueue pq;
Queue_entry qe[10];
Queue_entry item;
for(int i=0; i<10; i++) {
qe[i].data='A'+i;
qe[i].priority=1+i;
}
for(int i=0; i<5; i++) {
pq.append(qe[i]);
}
while (pq.empty()==false) {
pq.retrieve(item);
cout<<item.data<<":"<<item.priority<<endl;
pq.serve();
}
return 0;
}
大作业具体扣分如下
Testing □ -2 missing test for the method “empty ”
□ -2 missing test for the method “append”
□ -2 missing test for the method “serve”
□ -2 missing test for the method “retrieve”
□ -2 missing test for the constructor method
The methods in the Class “PriorityQueue”:
empty □ -5 not implemented/fail to explain the code □ -3 misfunction
append □ -10 not implemented/fail to explain the code □ -5 misfunction
serve □ -5 not implemented/fail to explain the code □ -3 misfunction
retrieve □ -5 not implemented/fail to explain the code □ -3 misfunction
PriorityQueue □ -5 not implemented/fail to explain the code □ -3 misfunction
The attributes in the Class “PriorityQueue”:
□ -5 add attributes
□ -5 remove attributes
Source Code 10 Marks(6+4)
Style:
□ -1 missing your details
□ -4 insufficient comments
□ -1 improper names
Separated 4 Files (Utility.h; PriorityQueue.h; PriorityQueue.cpp; main.cpp):
□ -3 miss 3 files
□ -2 miss 2 files
□ -1 miss 1 file
MrYKW