Advertisement
okpalan

heapq.c

Nov 9th, 2023 (edited)
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | Source Code | 0 0
  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.  
  4.     typedef struct {
  5.         int priority;
  6.         int value;
  7.     } Item;
  8.  
  9.     typedef struct {
  10.         Item* heap;
  11.         int capacity;
  12.         int size;
  13.     } HeapQueue;
  14.  
  15.     HeapQueue* hq_create(int capacity) {
  16.         HeapQueue* hq = (HeapQueue*)malloc(sizeof(HeapQueue));
  17.         hq->capacity = capacity;
  18.         hq->size = 0;
  19.         hq->heap = (Item*)malloc(capacity * sizeof(Item));
  20.         return hq;
  21.     }
  22.  
  23.     void hq_destroy(HeapQueue* hq) {
  24.         free(hq->heap);
  25.         free(hq);
  26.     }
  27.  
  28.     void hq_enqueue(HeapQueue* hq, int priority, int value) {
  29.         if (hq->size == hq->capacity) {
  30.             fprintf(stderr, "HeapQueue is full. Cannot enqueue.\n");
  31.             return;
  32.         }
  33.  
  34.         Item item = { priority, value };
  35.         int i = hq->size++;
  36.         int parent = (i - 1) / 2;
  37.  
  38.         while (i > 0 && hq->heap[parent].priority > item.priority) {
  39.             hq->heap[i] = hq->heap[parent];
  40.             i = parent;
  41.             parent = (i - 1) / 2;
  42.         }
  43.  
  44.         hq->heap[i] = item;
  45.     }
  46.  
  47.     Item hq_dequeue(HeapQueue* hq) {
  48.         if (hq->size == 0) {
  49.             fprintf(stderr, "HeapQueue is empty. Cannot dequeue.\n");
  50.             return (Item){0, 0};
  51.         }
  52.  
  53.         Item root = hq->heap[0];
  54.         Item last = hq->heap[--hq->size];
  55.         int i = 0;
  56.         int child = 1;
  57.  
  58.         while (child < hq->size) {
  59.             if (child + 1 < hq->size && hq->heap[child + 1].priority < hq->heap[child].priority) {
  60.                 child++;
  61.             }
  62.  
  63.             if (last.priority <= hq->heap[child].priority) {
  64.                 break;
  65.             }
  66.  
  67.             hq->heap[i] = hq->heap[child];
  68.             i = child;
  69.             child = 2 * i + 1;
  70.         }
  71.  
  72.         hq->heap[i] = last;
  73.         return root;
  74.     }
  75.  
  76.     int main() {
  77.         HeapQueue* hq = hq_create(10);
  78.  
  79.         hq_enqueue(hq, 2, 20);
  80.         hq_enqueue(hq, 4, 40);
  81.         hq_enqueue(hq, 1, 10);
  82.         hq_enqueue(hq, 3, 30);
  83.  
  84.         while (hq->size > 0) {
  85.             Item item = hq_dequeue(hq);
  86.             printf("Priority: %d, Value: %d\n", item.priority, item.value);
  87.         }
  88.  
  89.         hq_destroy(hq);
  90.         return 0;
  91.     }
  92.  
  93.  
Tags: QUEUE Heap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement