1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h>
typedef struct Node { int priority; char *course; } Node_t;
typedef struct Heap { Node_t *heap; int size; int capacity; } PriorityQueue_t;
PriorityQueue_t* initPriorityQueue(int capacity) { if (capacity <= 0) { return NULL; } PriorityQueue_t *queue = (PriorityQueue_t *)malloc(sizeof(PriorityQueue_t)); queue->heap = (Node_t *)malloc(capacity * sizeof(Node_t)); queue->size = 0; queue->capacity = capacity; return queue; }
int getParentIndex(int index) { return (index - 1) >> 1; }
int getLeftChildIndex(int index) { return (index << 1) + 1; }
int getRightChildIndex(int index) { return (index << 1) + 2; }
void swap(Node_t *s1, Node_t *s2) { Node_t temp = *s1; memcpy(s1, s2, sizeof(Node_t)); memcpy(s2, &temp, sizeof(Node_t)); }
void siftUp(PriorityQueue_t *queue, int index) { Node_t *heap = queue->heap; int parentIndex = getParentIndex(index); while ((index > 0) && (heap[index].priority > heap[parentIndex].priority)) { swap(&(heap[index]), &(heap[parentIndex])); index = parentIndex; parentIndex = getParentIndex(index); } }
void siftDown(PriorityQueue_t* queue, int index) { int maxIndex = index; int leftChildIndex = getLeftChildIndex(index); int rightChildIndex = getRightChildIndex(index);
if (leftChildIndex < queue->size && queue->heap[leftChildIndex].priority > queue->heap[maxIndex].priority) { maxIndex = leftChildIndex; }
if (rightChildIndex < queue->size && queue->heap[rightChildIndex].priority > queue->heap[maxIndex].priority) { maxIndex = rightChildIndex; }
if (index != maxIndex) { swap(&(queue->heap[index]), &(queue->heap[maxIndex])); siftDown(queue, maxIndex); } }
void push(PriorityQueue_t *queue, Node_t *node) { if (queue->size >= queue->capacity) { printf("Queue is full!\n"); return; } queue->heap[queue->size] = *node; siftUp(queue, queue->size); queue->size++; }
void peek(PriorityQueue_t *queue, Node_t* top) { if (queue->size <= 0) { memset(top, 0, sizeof(Node_t)); printf("Queue is empty!\n"); } else { memcpy(top, &(queue->heap[0]), sizeof(Node_t)); } }
void pop(PriorityQueue_t *queue, Node_t* top) { if (queue->size <= 0) { memset(top, 0, sizeof(Node_t)); printf("Queue is empty!\n"); } else { memcpy(top, &(queue->heap[0]), sizeof(Node_t)); queue->heap[0] = queue->heap[queue->size - 1]; queue->size--; siftDown(queue, 0); } }
void printPriorityQueue(PriorityQueue_t *queue) { printf("Priority queue:\n"); for (int i = 0; i < queue->size; ++i) { printf(" %s: %d\n", queue->heap[i].course, queue->heap[i].priority); } }
void destroyPriorityQueue(PriorityQueue_t *queue) { free(queue->heap); free(queue); }
int main(int argc, char *argv[]) { int size = 10, capacity = 10; Node_t node[] = { {2, "Computer Science"}, {7, "Discrete Mathematics"}, {26, "Data Structures and Algorithms"}, {25, "Operating Systems"}, {19, "C Programming Languages"}, {17, "Computer Networks"}, {1, "Database Management Systems"}, {90, "Artificial Intelligence"}, {3, "Linear Algebra"}, {36, "Calculus"} }; PriorityQueue_t *queue = initPriorityQueue(capacity); Node_t *topNode = (Node_t *)malloc(sizeof(Node_t)); memset(topNode, 0, sizeof(Node_t));
for (int i = 0; i < size; ++i) { push(queue, &(node[i])); } printPriorityQueue(queue);
peek(queue, topNode); if (topNode->course && topNode != 0) { printf("Priority queue top course: %s\n", topNode->course); }
pop(queue, topNode); if (topNode->course && topNode != 0) { printf("Priority queue pop course: %s\n", topNode->course); } printPriorityQueue(queue);
for (int i = 0; i < size; ++i) { pop(queue, topNode); if (topNode->course && topNode != 0) { printf("Priority queue pop course: %s\n", topNode->course); } } free(topNode); destroyPriorityQueue(queue); return 0; }
|