Advertisement
ahmad_zizo

dequeue

Apr 5th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. Node dequeue(Node heap[])
  2. {
  3.     if(array_size == 0)
  4.     {
  5.         printf("heap is empty\n");
  6.         return -123456789;
  7.     }
  8.     Node t;
  9.     t = heap[0];
  10.     Swap(&heap[0],&heap[array_size-1]);
  11.     array_size = array_size-1;
  12.     heapifyroot(heap);
  13.     return t;
  14. }
  15.  
  16. void heapifyroot(Node heap[])
  17. {
  18.     int i = 0;
  19.     while(i< (array_size)/2 && (heap[i].priority <= heap[2*i+1].priority || heap[i].priority <= heap[2*i+2].priority))
  20.     {
  21.         if(heap[2*i+1].priority < heap[2*i+2].priority)
  22.         {
  23.             Swap(&heap[0],&heap[2*i+2]);
  24.             i = 2*i+2;
  25.         }
  26.         else if(heap[2*i+1].priority > heap[2*i+2].priority)
  27.         {
  28.             Swap(&heap[0],&heap[2*i+1]);
  29.             i = 2*i+1;
  30.         }
  31.         else
  32.         {
  33.             if(heap[2*i+1].priority2 > heap[2*i+2].priority2)
  34.             {
  35.                 Swap(&heap[0],&heap[2*i+2]);
  36.                 i = 2*i+2;
  37.             }
  38.             else
  39.             {
  40.                 Swap(&heap[0],&heap[2*i+1]);
  41.                 i = 2*i+1;
  42.             }
  43.         }
  44.     }
  45.     return;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement