我正在使用堆执行赋值优先级队列 这些是每个命令的说明
void pop(int heapSize){
heap[0]=heap[heapSize-1]; //overwrite
heapSize--;
heapifyDown(0);
}
void heapifyDown(int insert){
int nextStep = insert;
right = (insert + 1)*2;
left=right-1;
if (left<heapSize){
nextStep=left;
}
if((right<heapSize)&&(heap[right].priority<heap[left].priority)){
nextStep=right;
}
while(heap[nextStep].priority < heap[insert].priority){
swap(heap[nextStep, heap[insert]]);
insert=nextStep;
}
right=()insert+1)*2;
leaft=right-1;
if(left<heapSize){
nextStep=left;
}
if((right<heapSize)&&heap[right].priority<heap[left].priority){
nextStep=right;
}
}
void deleteElement(heapElement, int element, int heapSize){
int index; // to remember the deleted node's position
for(int i=1; i <heapSize; i++){
if (heap[i] == element){
heap[i]=NULL;
index = i; // position of removed element;
break;
}
}
// move the last element(A[num_items_in_array]) in array to the
// position of removed element; heapify up/down according to
// priority
}
void reset(int element, int newP){
for(int i=1; i <heapSize; i++){
if (heap[i] == element){
heap[i].priority = newP;
heapifyDown(heap[i], newP);
heapifyUp(heap[i], newP);
}
}
}
void read(){
if(!heap[].empty()){
cout << heap[0];
}
}
void finalize(){
for(i=1; i<heapSize; i++){
cout << heap[i];
heap[i]=NULL;
}
}
int main()
{
int case, counter;
elment_priority node;
while(counter < 1000){
cin >> case;
cin >> node.element;
cin >> node.priority;
int addSum, deleteSum, resetSum, readSum, popSum,
finalizeSum, totalSum=0;
switch(case) {
case 1:
addElement(heapElement,node.element, heapSize);
addSum++;
case 2:
deleteElement(heapElement,node.element, heapSize);
deleteSum++:
case 3:
reset(node.element, node.priority);
resetSum++;
case 4:
read();
readSum++;
case 5:
pop();
popSum++;
case 6:
finalize();
finalizeSum++;
default:
cout<<"Cannot Process Command :"
+ case + node.element
+ node.priority + ": wrong command";
}
}
cout << "\n";
cout << "\n";
cout << "================================================================";
cout << addSum << "total add commands processed.\n";
cout << deleteSum << "total delete commands processed.\n";
cout << resetSum << "total reset commands processed.\n";
cout << readSum << "total read commands processed.\n";
cout << popSum << "total pop commands processed.\n";
cout << finalizeSum << "total finalize commands processed.\n";
cout << totalSum << "total commands processed.\n";
}