使用堆的优先级队列(我的代码怎么了?)

时间:2015-04-17 16:47:43

标签: c++ queue heap

我正在使用堆执行赋值优先级队列 这些是每个命令的说明

  1. 添加元素创建一个id为优先级的元素 将元素添加到队列中 不为此命令生成输出
  2. 删除元素从队列中删除了具有指定id的元素 不为此命令生成输出
  3. 重置优先级在优先级队列中找到具有指定标识的元素,将元素的优先级更改为请求的值,为此命令生成无输出
  4. 读取查询队列中的最高优先级元素 打印当前(总计)命令编号,元素ID和优先级 标准输出,后跟换行符,如下所示: 即:在命令编号5087处,队列顶部是元素99,优先级为1 此时不要打印“读取命令编号”
  5. 弹出查询最高优先级元素并将其从队列中删除 不为此命令生成输出
  6. finalize连续读取并弹出队列中剩余的所有元素 提供与每个剩余元素的读命令一致的输出

  7. 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";
    
    }
    

0 个答案:

没有答案