77 lines
2.1 KiB
C
77 lines
2.1 KiB
C
#include "lib/lib.h"
|
|
#include "lib/vec.h"
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
typedef struct Item {
|
|
int priority;
|
|
int data;
|
|
} Item;
|
|
|
|
DECL_VEC(ItemVec, Item);
|
|
|
|
// Really lazy linear sort, 🤷
|
|
void sort_queue(ItemVec *queue) {
|
|
for (int i = 0; i < queue->elem_count; i++) {
|
|
if (i + 1 >= queue->elem_count)
|
|
break;
|
|
Item curr_element = *ItemVec_get(queue, i);
|
|
Item next_element = *ItemVec_get(queue, i + 1);
|
|
if (curr_element.priority > next_element.priority) {
|
|
queue->data[i] = next_element;
|
|
queue->data[i + 1] = curr_element;
|
|
}
|
|
}
|
|
}
|
|
|
|
void print_item(Item *item) {
|
|
printf("(Element: %d, Priority: %d)\n", item->data, item->priority);
|
|
}
|
|
|
|
void print_queue(ItemVec *queue) {
|
|
for (int i = 0; i < queue->elem_count; i++) {
|
|
Item *item = ItemVec_get(queue, i);
|
|
print_item(item);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
print_header();
|
|
printf("--- P-Queue: 5 = Highest, 1 = Lowest ---\n");
|
|
int queue_len = 0;
|
|
printf("Enter P-Queue Length = ");
|
|
scanf("%d", &queue_len);
|
|
ItemVec queue = ItemVec_new(queue_len);
|
|
|
|
for (int idx = 0; idx < queue_len; idx++) {
|
|
int element_num = idx + 1;
|
|
int element = 0;
|
|
int element_priority = 1;
|
|
|
|
printf("Enter element %d: ", element_num);
|
|
scanf("%d", &element);
|
|
printf("Enter priority of element %d: ", element_num);
|
|
scanf("%d", &element_priority);
|
|
Item item = {.priority = element_priority, .data = element};
|
|
ItemVec_push(&queue, item);
|
|
}
|
|
|
|
sort_queue(&queue);
|
|
printf("--- Priority Queue Full ---\n");
|
|
printf("Priority Queue Before Dequeue (Sorted by Ascending Priority) :\n");
|
|
print_queue(&queue);
|
|
while (queue.elem_count != 0) {
|
|
printf("---------------------------\n");
|
|
printf("Dequeue (1 for yes, 0 for exit): ");
|
|
int option = 0;
|
|
scanf("%d", &option);
|
|
if (option == 0)
|
|
break;
|
|
ItemVec_pop(&queue);
|
|
printf("Priority Queue After Dequeue :\n");
|
|
print_queue(&queue);
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|