First commit

This commit is contained in:
mossfet 2023-03-31 12:27:08 +01:00
commit 4056cf9293
4 changed files with 134 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
a.out

17
main.c Normal file
View file

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include "priority_queue.h"
int main (int argc, char** argv) {
PriorityQueue priority_queue = { (int*)calloc(10, sizeof(int)), 0, 5};
int i = 0;
while (true) {
insert(&priority_queue, i);
if (i % 100000 == 0) {
printf("%i\n", i);
}
i++;
}
return 0;
}

89
priority_queue.c Normal file
View file

@ -0,0 +1,89 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "priority_queue.h"
void insert(PriorityQueue *priority_queue, int item) {
if (priority_queue->max_item == priority_queue->size - 1) {
printf("Reallocating from %i to %i!\n", priority_queue->size, priority_queue->size*2);
priority_queue->array = realloc(priority_queue->array, priority_queue->size*2*sizeof(int));
if (priority_queue->array == NULL) {
printf("That's a null pointer!\n");
}
priority_queue->size *= 2;
}
priority_queue->array[priority_queue->max_item] = item;
bubble_up(priority_queue, priority_queue->max_item);
priority_queue->max_item++;
}
int extract (PriorityQueue *priority_queue) {
int item = priority_queue->array[0];
priority_queue->array[0] = priority_queue->array[priority_queue->max_item - 1];
bubble_down(priority_queue, 0);
return item;
}
size_t parent (size_t index) {
return (index-1)/2;
}
size_t l_child (size_t index) {
return (2*index)+1;
}
size_t r_child (size_t index) {
return (2*index)+2;
}
void bubble_up (PriorityQueue *priority_queue, size_t index) {
if (index == 0) {
return;
}
if (compare(priority_queue, index, parent(index))) {
swap_parent(priority_queue, index);
bubble_up (priority_queue, parent(index));
}
}
void bubble_down (PriorityQueue *priority_queue, size_t index) {
size_t min = index;
size_t left = l_child (index);
size_t right = r_child (index);
size_t max_item = priority_queue->max_item;
if (left < max_item && compare(priority_queue, left, index)) {
min = left;
}
if (right < max_item && compare(priority_queue, right, min)) {
min = right;
}
if (min != index) {
swap_parent(priority_queue, min);
bubble_down(priority_queue, min);
}
}
void print_queue (PriorityQueue *priority_queue) {
for (int i = 0; i < priority_queue->size; i++) {
printf("%i\n", priority_queue->array[i]);
}
}
void swap_parent (PriorityQueue *priority_queue, size_t lower_index) {
int temp = priority_queue->array[lower_index];
priority_queue->array[lower_index] = priority_queue->array[parent(lower_index)];
priority_queue->array[parent(lower_index)] = temp;
}
bool compare (PriorityQueue *priority_queue, size_t first_index, size_t second_index) {
if (priority_queue->array[first_index] < priority_queue->array[second_index]) {
return true;
} else {
return false;
}
}

27
priority_queue.h Normal file
View file

@ -0,0 +1,27 @@
#include <stdbool.h>
struct PriorityQueue {
int* array;
// The NEXT item to be added
size_t max_item;
size_t size;
};
typedef struct PriorityQueue PriorityQueue;
void insert(PriorityQueue *priority_queue, int item);
int extract (PriorityQueue *priority_queue);
size_t parent (size_t index);
size_t l_child (size_t index);
size_t r_child (size_t index);
void bubble_up (PriorityQueue *priority_queue, size_t index);
void bubble_down (PriorityQueue *priority_queue, size_t index);
void print_queue (PriorityQueue *priority_queue);
void swap_parent (PriorityQueue *priority_queue, size_t lower_index);
bool compare (PriorityQueue *priority_queue, size_t first_index, size_t second_index);