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 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "priority_queue.h"
// DO NOT COPY CODE FROM THIS FILE INTO YOUR OWN CODE. We are providing this
// only to clarify how the pieces are supposed to fit together. It would not
// be a good basis for your test_priority_queue.c.
static int _cmp_strings_by_length(void const* a_lhs, void const* a_rhs) {
size_t len_lhs = strlen(a_lhs); // No typecast needed! Never use typedcast unless:
size_t len_rhs = strlen(a_rhs); // (a) necessary, (b) safe, and (c) well-understood.
return len_lhs - len_rhs; // shortest string comes forst
}
static char* _copy_string(const char* src_string) {
size_t num_chars = strlen(src_string);
char* dst_string = malloc(sizeof(*dst_string) * (num_chars + 1));
return strcpy(dst_string, src_string);
}
static void _destroy_string(void* string) {
free(string);
}
int main(int argc, char* argv[]) {
// Create a priority queue of four strings, ordered by length (ascending).
Node* head = NULL; // size 0
pq_enqueue(&head, _copy_string("Reginald"), _cmp_strings_by_length); // size 1
pq_enqueue(&head, _copy_string("was"), _cmp_strings_by_length); // size 2
pq_enqueue(&head, _copy_string("a"), _cmp_strings_by_length); // size 3
pq_enqueue(&head, _copy_string("hatter"), _cmp_strings_by_length); // size 4
// Print contents of the list: a was hatter Reginald
for(Node* curr = head; curr != NULL; curr = curr -> next) {
printf("%s\n", (char*)(curr -> a_value));
}
// Deallocate all memory used by the list, including each node's payload.
destroy_list(&head, _destroy_string);
return EXIT_SUCCESS;
}
/* ______
* Output
* ‾‾‾‾‾‾
* a
* was
* hatter
* Reginald
*/
#define DEMO_HUFFMAN_C_V1
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.