Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,22 +51,18 @@ sudo make uninstall
```C
#include "array.h"
#include <stdio.h>
#include <assert.h>

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");

}
```

Expand Down
8 changes: 4 additions & 4 deletions src/array/array.c
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions src/array/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
#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
@collection = collection to add to
@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
Expand All @@ -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
16 changes: 10 additions & 6 deletions src/array/test_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand All @@ -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;
Expand All @@ -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;
}
9 changes: 6 additions & 3 deletions src/array/test_program_take_too_much_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/data_type/data_type.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "data_type.h"
#include <stdio.h>

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)
{
Expand Down
6 changes: 3 additions & 3 deletions src/data_type/data_type.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TYPE_H
#define TYPE_H

typedef enum data_type
typedef enum DataType
{
CHAR,
SIGNED_CHAR,
Expand All @@ -14,14 +14,14 @@ typedef enum data_type
FLOAT,
DOUBLE,
LONG_DOUBLE
} data_type;
} DataType;

/*
saves value at supplied memory address
@type = data type of values to store
@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
14 changes: 7 additions & 7 deletions src/linked_list/linked_list.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "linked_list.h"
#include <stdlib.h>

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;
Expand All @@ -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);
Expand Down
29 changes: 18 additions & 11 deletions src/linked_list/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions src/linked_list/node.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/linked_list/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/linked_list/test_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down