Advertisement
ahmad_zizo

HEAP

Mar 20th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_SIZE 500
  5.  
  6. int array_size = 0;
  7.  
  8. typedef struct
  9. {
  10. int priority,value;
  11. }Node;
  12.  
  13. void initialize(Node heap[])
  14. {
  15. array_size = 0;
  16. return;
  17. }
  18.  
  19. void heapify(Node heap[], int i /*index*/)
  20. {
  21. int j;
  22. Node temp;
  23. j = i/2;
  24. while (i > 0 && heap[j].priority < heap[i].priority)
  25. {
  26. temp = heap[j];
  27. heap[j] = heap[i];
  28. heap[i] = temp;
  29. i=j;
  30. j/=2;
  31. }
  32. return;
  33. }
  34.  
  35. void enqueue(Node heap[],int priority,int value)
  36. {
  37. if(array_size == MAX_SIZE)
  38. {
  39. printf("you reached max size");
  40. return;
  41. }
  42. heap[array_size].priority = priority;
  43. heap[array_size].value = value;
  44. array_size++;
  45. heapify(heap,array_size);
  46. return;
  47. }
  48.  
  49. int main()
  50. {
  51. Node heap[MAX_SIZE];
  52. initialize(heap);
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement