diff --git a/docs/generate_docs.sh b/docs/generate_docs.sh index 0bd309e..19aaf95 100755 --- a/docs/generate_docs.sh +++ b/docs/generate_docs.sh @@ -4,6 +4,9 @@ # TODO: create options for generating different file types (pdf, html or utf8 to console/file). # Prompt user at the start for what type they want. +# Specify the file paths, in the order where they should be built. +filepaths=('nodes/single_node.h.ms' 'nodes/double_node.h.ms' 'nodes/tree_node.h.ms' 'strings/strzcpy.ms') + # Prompt the user for the option they want (pdf/html/utf8). echo "Please choose an output option: pdf, html or utf8." select choice in "pdf" "html" "utf8"; do @@ -28,9 +31,6 @@ done # Get the initial root. root_directory=$(pwd) -# Specify the file paths, in the order where they should be built. -filepaths=('nodes/single_node.h.ms') - # Loop over the paths: change directory, build, go back up to the root directory. for i in $filepaths; do # Get the current directory that we should change into, from the current array variable. @@ -49,7 +49,7 @@ for i in $filepaths; do # Create the output file. touch $output_file # Generate docs. - groff -ms -Tpdf $input_file > $output_file + groff -ms -T$filetype $input_file > $output_file # Go back up to the root. cd $root_directory diff --git a/progress_and_notes b/progress_and_notes index 7f353e3..c05f5bd 100644 --- a/progress_and_notes +++ b/progress_and_notes @@ -1,4 +1,4 @@ -Everything is implemented and tested. +Make sure everything is implemented and tested. TODO: imlement better tests, maybe even use a testing framework? Future goals: @@ -6,4 +6,26 @@ Implement more data structures, such as hash tables, hash maps, more trees. Implement algorithms to go with said DSs. Implement generic algorithms (start from sort and search). -Write documentation - probably using groff to be able to make man pages. +Write documentation. + +Currently implemented: +Single node. +Double linked node. +Tree node. +Linked list. +Double linked list. +Stack. +Safe string copy (not tested - not a priority). +Bubble Sort (not tested). + +To be implemented: +Queue. +Double-ended queue. +Hashmap. +Heap. +BST - Binary Search Tree. +AVL trees. +Red-Black trees. +AA Trees (?). +Circular buffer (?). +Array list (?). diff --git a/sorting_algorithms/bubble_sort/bubble_sort.c b/sorting_algorithms/bubble_sort/bubble_sort.c index 1a3524d..42e27a9 100644 --- a/sorting_algorithms/bubble_sort/bubble_sort.c +++ b/sorting_algorithms/bubble_sort/bubble_sort.c @@ -1,5 +1,65 @@ -/* This header defines the function for bubble sort. */ -#ifndef BUBBLE_SORT_H -#define BUBBLE_SORT_H -void** bubble_sort(void** array, void (*comparison_function)()); -#endif /* BUBBLE_SORT_H */ +/* This file has the implementation of the function for bubble sort. */ +/* Include guards + include statements. */ +#ifndef BUBBLE_SORT_C +#define BUBBLE_SORT_C +#include +#include "bubble_sort.h" +#endif /* BUBBLE_SORT_C */ + +/* Function implementation. */ +/* Note that if the comparison function is NULL, then the regular comparison operator is used <. */ +/* Note that if copy is set to 0, then the array is sorted in-place, otherwise a sorted copy +* is what gets returned (after allocation), leaving the original intact. */ +/* Note that the comparison function should return 1 if the second argument is larger than or +* equal to the first, otherwise zero. */ +/* The array's length should be passed in the length argument. */ +/* The function returns a NULL pointer on error, caller must check! */ +void** bubble_sort(const void** array, const void (*comparison_function)(), const int length, const int copy) { + /* Sanity check. */ + if (array == NULL) + return NULL; + + /* Local variables. */ + int i, j; + void **ptr, void *tmp; + + /* Check if to allocate a copy. */ + if (copy) { + /* Allocate the array. */ + ptr = (void**) malloc(length * sizeof(void*)); + + /* Check if the allocation failed. */ + if (ptr == NULL) + return NULL; + /* Copy the arrays' contents. */ + memcpy(ptr, array, length * sizeof(void*)); + } else + ptr = array; + + /* Sort according to ptr, and based on the employed comparison function. */ + if (comparison_function != NULL) + for (i = 0; i < length; i++) + for (j = 0; j < length - 1; j++) { + /* Check if to swap. */ + if (!comparison_function(ptr + j, ptr + j + 1)) { + /* Swap. */ + tmp = ptr[j]; + ptr[j] = ptr[j + 1]; + ptr[j + 1] = tmp; + } + } + else + for (i = 0; i < length; i++) + for (j = 0; j < length; j++) { + /* Check if to swap. */ + if (ptr[j] > ptr[j + 1]) { + /* Swap. */ + tmp = ptr[j]; + ptr[j] = ptr[j + 1]; + ptr[j + 1] = tmp; + } + } + + /* Return the result. */ + return ptr; +} diff --git a/sorting_algorithms/bubble_sort/bubble_sort.h b/sorting_algorithms/bubble_sort/bubble_sort.h index 1a3524d..4b14460 100644 --- a/sorting_algorithms/bubble_sort/bubble_sort.h +++ b/sorting_algorithms/bubble_sort/bubble_sort.h @@ -1,5 +1,5 @@ /* This header defines the function for bubble sort. */ #ifndef BUBBLE_SORT_H #define BUBBLE_SORT_H -void** bubble_sort(void** array, void (*comparison_function)()); +void** bubble_sort(const void** array, const void (*comparison_function)(), const int length, const int copy); #endif /* BUBBLE_SORT_H */