From bdd4327edfd6ea5e0faf5586d10c8ef4814ca5ad Mon Sep 17 00:00:00 2001 From: Praise Tompane Date: Wed, 14 May 2025 14:08:31 +0200 Subject: [PATCH] refactor: clean up interface to an intuitive one which also removes the to allow multiple data structure in the same module. --- README.md | 10 ++----- src/array/array.c | 8 ++--- src/array/array.h | 18 ++++++------ src/array/test_program.c | 16 ++++++---- src/array/test_program_take_too_much_memory.c | 9 ++++-- src/data_type/data_type.c | 2 +- src/data_type/data_type.h | 6 ++-- src/linked_list/linked_list.c | 14 ++++----- src/linked_list/linked_list.h | 29 ++++++++++++------- src/linked_list/node.c | 4 +-- src/linked_list/node.h | 10 +++---- src/linked_list/test_program.c | 2 +- 12 files changed, 69 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index fb4680c..5ebf2ed 100755 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![build status](https://github.com/praisetompane/algorithms_and_data_structures/actions/workflows/algorithms_and_data_structures.yaml/badge.svg) ## Objectives -- Re-implement foundational data structures and algorithms at a lower level of the stack for educational purposes. +- Implement foundational data structures and algorithms at a lower level of the stack for educational purposes. ## Project Structure @@ -51,22 +51,18 @@ sudo make uninstall ```C #include "array.h" #include -#include int main() { - printf("Test creating, adding and reading from Integer array\n"); - array numbers = new (INT, 10); - assert(numbers.length == 0); + printf("Test creating, adding and reading from Integer Array\n"); + Array numbers = array(INT, 10); int number = 2; add(&numbers, 0, &number); int read_int; read(&numbers, 0, &read_int); - assert(read_int == number); printf("\n"); - } ``` diff --git a/src/array/array.c b/src/array/array.c index 347a465..31a1e14 100755 --- a/src/array/array.c +++ b/src/array/array.c @@ -1,9 +1,9 @@ #include "../memory_cell/memory_allocator.h" #include "array.h" -array new(data_type type, int size) +Array array(DataType type, int size) { - array temp = {NULL, 0, type}; + Array temp = {NULL, 0, type}; switch (type) { case CHAR: @@ -43,14 +43,14 @@ array new(data_type type, int size) return temp; } -void add(array *collection, int index, void *value) +void add(Array *collection, int index, void *value) { void *target_address = collection->first_address + index; save_data_type(collection->type, value, target_address); collection->length += collection->length + 1; } -void read(array *collection, int index, void *destination) +void read(Array *collection, int index, void *destination) { void *targetAddress = collection->first_address + index; switch (collection->type) diff --git a/src/array/array.h b/src/array/array.h index 7b1c80d..d1f48d5 100755 --- a/src/array/array.h +++ b/src/array/array.h @@ -4,23 +4,23 @@ #define ARRAY_H /* - defined a type called 'struct array' - gave it an alias array + defined a type called 'struct Array' + gave it an alias Array */ -typedef struct array +typedef struct Array { void *first_address; int length; - data_type type; -} array; + DataType type; +} Array; /* - creates and returns an array for the specified: + creates and returns an Array for the specified: @type = data type of values to store @size = number of items to be stored type variable-name [size] */ -array new(data_type type, int size); +Array array(DataType type, int size); /* store a value in the collection @@ -28,7 +28,7 @@ array new(data_type type, int size); @index = index to store the in @value = value to store */ -void add(array *collection, int index, void *value); +void add(Array *collection, int index, void *value); /* read an item @@ -37,6 +37,6 @@ void add(array *collection, int index, void *value); @*destination = pointer to memory address to read into */ -void read(array *collection, int index, void *destination); +void read(Array *collection, int index, void *destination); #endif \ No newline at end of file diff --git a/src/array/test_program.c b/src/array/test_program.c index 22d17a8..0b7ecfa 100755 --- a/src/array/test_program.c +++ b/src/array/test_program.c @@ -4,8 +4,8 @@ int main() { - printf("Test creating, adding and reading from Integer array\n"); - array numbers = new (INT, 10); + printf("Test creating, adding and reading from Integer Array\n"); + Array numbers = array(INT, 10); assert(numbers.length == 0); int number = 2; @@ -16,9 +16,10 @@ int main() read(&numbers, 0, &read_int); assert(read_int == number); printf("\n"); + free(numbers.first_address); - printf("Test creating, adding and reading from Character array\n"); - array alphabets = new (CHAR, 10); + printf("Test creating, adding and reading from Character Array\n"); + Array alphabets = array(CHAR, 10); assert(alphabets.length == 0); char letter = 'A'; @@ -29,9 +30,10 @@ int main() read(&alphabets, 0, &read_char); assert(read_char == letter); printf("\n"); + free(alphabets.first_address); - printf("Test creating, adding and reading from Floats array\n"); - array stock_prices = new (FLOAT, 10); + printf("Test creating, adding and reading from Floats Array\n"); + Array stock_prices = array(FLOAT, 10); assert(stock_prices.length == 0); float rmb_price = 122.34; @@ -42,5 +44,7 @@ int main() read(&stock_prices, 0, &read_float); assert(read_float == rmb_price); printf("\n"); + free(stock_prices.first_address); + return 0; } \ No newline at end of file diff --git a/src/array/test_program_take_too_much_memory.c b/src/array/test_program_take_too_much_memory.c index 6518239..a38096d 100755 --- a/src/array/test_program_take_too_much_memory.c +++ b/src/array/test_program_take_too_much_memory.c @@ -3,17 +3,20 @@ int main() { - array numbers = new (DOUBLE, 10); + Array numbers = array(DOUBLE, 10); printf("number of customer ages %d \n", numbers.length); printf("first address %ld\n", (long int)numbers.first_address); + printf("adding 2 at index 0\n"); - add(&numbers, 0, 2); + int number = 2; + add(&numbers, 0, &number); + int read_int; read(&numbers, 0, &read_int); printf("reading a value at index 0 is %d \n", read_int); printf("size of double %ld", sizeof(long double)); - array BIGARRAY = new (LONG_DOUBLE, 33554433); + Array BIGARRAY = array(LONG_DOUBLE, 33554433); while (1 == 1) { printf("Just chilling, check memory consumption"); diff --git a/src/data_type/data_type.c b/src/data_type/data_type.c index 5932eaa..1d04d3a 100755 --- a/src/data_type/data_type.c +++ b/src/data_type/data_type.c @@ -1,7 +1,7 @@ #include "data_type.h" #include -void save_data_type(data_type type, void *value, void *target_address) +void save_data_type(DataType type, void *value, void *target_address) { switch (type) { diff --git a/src/data_type/data_type.h b/src/data_type/data_type.h index 971a5f9..944d49f 100755 --- a/src/data_type/data_type.h +++ b/src/data_type/data_type.h @@ -1,7 +1,7 @@ #ifndef TYPE_H #define TYPE_H -typedef enum data_type +typedef enum DataType { CHAR, SIGNED_CHAR, @@ -14,7 +14,7 @@ typedef enum data_type FLOAT, DOUBLE, LONG_DOUBLE -} data_type; +} DataType; /* saves value at supplied memory address @@ -22,6 +22,6 @@ typedef enum data_type @value = value to store @target_address = memory address to store the value */ -void save_data_type(data_type type, void *value, void *target_address); +void save_data_type(DataType type, void *value, void *target_address); #endif \ No newline at end of file diff --git a/src/linked_list/linked_list.c b/src/linked_list/linked_list.c index 54c354e..085753d 100755 --- a/src/linked_list/linked_list.c +++ b/src/linked_list/linked_list.c @@ -1,20 +1,20 @@ #include "linked_list.h" #include -linked_list new(data_type type) +LinkedList linkedlist(DataType type) { - linked_list temp = {NULL, 0, type}; + LinkedList temp = {NULL, 0, type}; return temp; } -void add(linked_list *collection, void *value) +void add(LinkedList *collection, void *value) { - node *n = newnode(value, NULL); + Node *n = newnode(value, NULL); save_data_type(collection->type, value, n->value); if (collection->head != NULL) { - node *currentnode = collection->head; + Node *currentnode = collection->head; while (currentnode->next != NULL) currentnode = currentnode->next; currentnode->next = n; @@ -27,11 +27,11 @@ void add(linked_list *collection, void *value) collection->length += 1; } -void printlinked_list(linked_list collection) +void print(LinkedList collection) { if (collection.head != NULL) { - node *currentnode = collection.head; + Node *currentnode = collection.head; while (currentnode->next != NULL) { printf("%d->", *(int *)currentnode->value); diff --git a/src/linked_list/linked_list.h b/src/linked_list/linked_list.h index 55b7a2c..532f27b 100755 --- a/src/linked_list/linked_list.h +++ b/src/linked_list/linked_list.h @@ -4,18 +4,18 @@ #ifndef LINKED_LIST_H #define LINKED_LIST_H -typedef struct linked_list +typedef struct LinkedList { - node *head; + Node *head; int length; - data_type type; -} linked_list; + DataType type; +} LinkedList; /* creates and returns a linked list for the specified @type = data type of values to store§3 #3 */ -linked_list new(data_type type); +LinkedList linkedlist(DataType type); /* add a node at the end of linked list @@ -26,38 +26,45 @@ linked_list new(data_type type); create @newnode with @value set current @currentnode->next to @newnode */ -void add(linked_list *collection, void *value); +void add(LinkedList *collection, void *value); /* store an item at the top @value = value to store */ -linked_list add_to_top(void *value); +LinkedList add_to_top(void *value); /* read an item at the end @collection = collection to read from @value = node to read */ -node read(linked_list *collection, void *value); +Node read(LinkedList *collection, void *value); /* read an item at the top @collection = collection to read from @value = node to read */ -node read_from_top(linked_list *collection, void *value); +Node read_from_top(LinkedList *collection, void *value); /* delete an item at the end @collection = collection to read from @value = node to read */ -node delete(linked_list *collection, void *value); +Node delete(LinkedList *collection, void *value); /* delete an item at the top @collection = collection to read from @value = node to read */ -node delete_from_top(linked_list *collection, void *value); +Node delete_from_top(LinkedList *collection, void *value); + +/* + print the collection + @collection = collection to print +*/ +void print(LinkedList collection); + #endif \ No newline at end of file diff --git a/src/linked_list/node.c b/src/linked_list/node.c index 2348a6d..dfbf197 100755 --- a/src/linked_list/node.c +++ b/src/linked_list/node.c @@ -1,9 +1,9 @@ #include "../memory_cell/memory_allocator.h" #include "node.h" -node *newnode(void *value, node *next) +Node *newnode(void *value, Node *next) { - node *n = (node *)allocate_memory(sizeof(node)); + Node *n = (Node *)allocate_memory(sizeof(Node)); n->value = value; n->next = next; return n; diff --git a/src/linked_list/node.h b/src/linked_list/node.h index 445f7ae..fd83b17 100755 --- a/src/linked_list/node.h +++ b/src/linked_list/node.h @@ -3,22 +3,22 @@ #ifndef NODE_H #define NODE_H -typedef struct node +typedef struct Node { void *value; - struct node *next; -} node; + struct Node *next; +} Node; /* create a new node */ -node *newnode(); +Node *newnode(); /* create a new node @value = value for node @next = the node's successor */ -node *newnode(void *value, node *next); +Node *newnode(void *value, Node *next); #endif \ No newline at end of file diff --git a/src/linked_list/test_program.c b/src/linked_list/test_program.c index a3f0593..005b29c 100755 --- a/src/linked_list/test_program.c +++ b/src/linked_list/test_program.c @@ -5,7 +5,7 @@ int main() { printf("Test creating Integer linked_list\n"); - linked_list numbers = new (INT); + LinkedList numbers = linkedlist(INT); assert(numbers.head == NULL); assert(numbers.length == 0); assert(numbers.type == INT);