Skip to content

Commit

Permalink
Merge pull request #30 from urielgarciarivas/main
Browse files Browse the repository at this point in the history
Somewhat major changes
  • Loading branch information
urielgarciarivas authored May 15, 2024
2 parents b094922 + 4f4e257 commit 46a923a
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 124 deletions.
13 changes: 10 additions & 3 deletions DataStructures/inc/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
#ifndef __ZNG_ARRAY_H__
#define __ZNG_ARRAY_H__

#include <stdbool.h>
#include <stddef.h>

typedef struct __zng_array {
int* data;
size_t size;
} array;

// Every new array needs to be deleted using delete_array(...).
extern array* new_array(int value);
// Every new array needs to be deleted using deallocate_array(...).
extern array* allocate_array(int value);
extern array* allocate_empty_array();
extern void deallocate_array(array* arr);

extern void add_to_array(array*const arr, int value);

extern void delete_array(array* arr);
extern bool exist_in_array(const array*const arr, int target);

extern void delete_last_array(array* arr);

extern bool is_empty_array(const array*const arr);

#endif // __ZNG_ARRAY_H__
14 changes: 7 additions & 7 deletions DataStructures/inc/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ typedef struct __zng_ll {
linked_list_node* tail;
} linked_list;

// Every new linked list needs to be deleted using delete_linked_list(...).
extern linked_list* new_linked_list(int value);
// Every new linked list needs to be deleted using deallocate_linked_list(...).
extern linked_list* allocate_linked_list(int value);
extern linked_list* allocate_empty_linked_list();
extern void deallocate_linked_list(linked_list* list);

// Example output to screen: "list = {1, 2, 3, 4, 5};"
extern void print_linked_list(const linked_list*const list);

// Linear time, stops when finding the first occurence.
extern bool exist_in_linked_list(const linked_list*const list, int value);

// TODO: Finish head and tail functions.
extern void add_to_linked_list(linked_list*const list, int value);

extern void delete_linked_list(linked_list* list);
extern void add_head_linked_list(linked_list*const list, int value);
extern void add_tail_linked_list(linked_list*const list, int value);

// Deletes first occurence of 'target' in the linked list.
// It frees its memory.
extern void delete_single_match_linked_list(linked_list* list, int target);

extern void delete_all_match_linked_list(linked_list* list, int target);

extern void delete_head_linked_list(linked_list* list);

extern void delete_tail_linked_list(linked_list* list);

// Linear time.
Expand Down
21 changes: 12 additions & 9 deletions DataStructures/inc/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@

#include "../../Terminal/outputstream.h"

#define ALLOCATE(type, pointer) \
pointer = (type *) malloc(sizeof(type)); \
if ((pointer) == NULL) { \
STDERR_RED("ERROR: Out of memory.\n"); \
EXIT(1); \
}
#define ALLOCATE(type, pointer) \
do { \
(pointer) = (type *) malloc(sizeof(type)); \
if ((pointer) == NULL) { \
STDERR_RED("ERROR: Out of memory.\n"); \
} \
} while (0);

#define DEALLOCATE(pointer) \
free((pointer)); \
pointer = NULL;
#define DEALLOCATE(pointer) \
do { \
free((pointer)); \
(pointer) = NULL; \
} while (0);

#endif // __ZNG_MEMORY_H__
8 changes: 3 additions & 5 deletions DataStructures/inc/sort_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include <stdbool.h>
#include <stdlib.h>

#include "memory.h"

typedef struct __zng_ss_node {
int value;
int left_depth;
Expand All @@ -37,16 +35,16 @@ typedef struct __zng_ss {
sort_set_node* root;
} sort_set;

extern sort_set* new_sort_set(int value);
extern sort_set* allocate_sort_set(int value);
extern sort_set* allocate_empty_sort_set();
extern void deallocate_sort_set(sort_set* list);

extern void add_to_sort_set(sort_set*const set, int value);

// Time: O(log(n))
// Space: O(1)
extern bool exist_in_sort_set(const sort_set*const set, int target);

extern void delete_sort_set(sort_set* list);

extern bool is_empty_sort_set(const sort_set*const set);

#endif // __ZNG_SORT_SET_H__
52 changes: 34 additions & 18 deletions DataStructures/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,47 @@
#
# https://github.com/zoningorg/zoning/blob/main/LICENSE

# Compilation.
CC = gcc
CFLAGS = -Wall -Wextra -Werror -Wpedantic -I./inc/
INC = ./inc/array.h ./inc/hash_map.h ./inc/hash_set.h ./inc/linked_list.h \
./inc/sort_map.h ./inc/sort_set.h
SRC = $(patsubst ./inc/%.h,./src/%.c,$(INC))
OBJ = $(patsubst ./inc/%.h,./obj/%.o,$(INC))
TESTS = array_test hash_map_test hash_set_test linked_list_test sort_map_test \
sort_set_test

# All targets. This assumes there exists a .h, .c, and _test.c per target.
TARGETS = array \
hash_map \
hash_set \
linked_list \
sort_map \
sort_set

# Object files.
OBJ_DIR = ./obj
OBJ = $(patsubst %,$(OBJ_DIR)/%.o,$(TARGETS))

# Binary files.
SRC_DIR = ./test/src
BIN_DIR = ./test/bin
BIN = $(patsubst %,$(BIN_DIR)/%_test,$(TARGETS))

# Top-level rule.
all: test

test: $(TESTS)
for TEST in $(TESTS); do \
./$$TEST; \
# General rule for every object file.
$(OBJ_DIR)/%.o: ./src/%.c
$(CC) $(CFLAGS) -c $< -o $@

# General rule for every test binary.
$(BIN_DIR)/%_test: $(SRC_DIR)/%_test.c $(OBJ_DIR)/%.o
$(CC) $(CFLAGS) $^ -o $@

# Automatic rule to run all tests in BIN.
# Stop if one test fails.
test: $(BIN)
for test_binary in $(BIN); do \
./$$test_binary; \
[[ $$? -ne 0 ]] && break; \
done; \
make clean

./obj/%.o: ./src/%.c
$(CC) $(CFLAGS) -c $^ -o $@

$(TESTS): % : ./test/%.c $(OBJ)
$(CC) $(CFLAGS) $< -o $@ $(OBJ)

# Remove created files.
clean:
rm $(TESTS) $(OBJ)

# $(wildcard *.o)
rm -f $(OBJ) $(BIN)
28 changes: 26 additions & 2 deletions DataStructures/src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "../inc/array.h"
#include "../inc/memory.h"

array* new_array(int value) {
inline array* allocate_array(int value) {
array* response;

ALLOCATE(array, response);
Expand All @@ -30,7 +30,17 @@ array* new_array(int value) {
return response;
}

inline void delete_array(array* arr) {
inline array* allocate_empty_array() {
array* response;

ALLOCATE(array, response);
response->size = 0;
response->data = NULL;

return response;
}

inline void deallocate_array(array* arr) {
if (arr == NULL) {
return;
}
Expand All @@ -41,3 +51,17 @@ inline void delete_array(array* arr) {

DEALLOCATE(arr);
}

bool exist_in_array(const array*const arr, int target) {
for (size_t i = 0; i < arr->size; ++i) {
if (arr->data[i] == target) {
return true;
}
}

return false;
}

inline bool is_empty_array(const array*const arr) {
return arr == NULL || (arr->data == NULL && arr->size == 0);
}
64 changes: 38 additions & 26 deletions DataStructures/src/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "../inc/linked_list.h"
#include "../inc/memory.h"

inline linked_list* new_linked_list(int value) {
inline linked_list* allocate_linked_list(int value) {
linked_list* response;

ALLOCATE(linked_list, response);
Expand All @@ -34,6 +34,36 @@ inline linked_list* new_linked_list(int value) {
return response;
}

inline linked_list* allocate_empty_linked_list() {
linked_list* response;

ALLOCATE(linked_list, response);
response->size = 0;
response->head = NULL;
response->tail = NULL;

return response;
}

void deallocate_linked_list(linked_list* list) {
if (list == NULL) {
return;
}

linked_list_node* node;
linked_list_node* next;

for (node = list->head; node != NULL; node = next) {
next = node->next;
DEALLOCATE(node);
}

list->head = NULL;
list->tail = NULL;
list->size = 0;
DEALLOCATE(list);
}

void print_linked_list(const linked_list*const list) {
if (list == NULL) {
printf("list = {};\n");
Expand Down Expand Up @@ -73,7 +103,7 @@ bool exist_in_linked_list(const linked_list*const list, int value) {
return false;
}

// TODO(Garz4): Fix edge case when list is not NULL, but its head and tail are.
// TODO: Fix edge case when list is not NULL, but its head and tail are.
inline void add_to_linked_list(linked_list*const list, int value) {
if (list == NULL) {
return;
Expand All @@ -86,25 +116,6 @@ inline void add_to_linked_list(linked_list*const list, int value) {
list->size++;
}

void delete_linked_list(linked_list* list) {
if (list == NULL) {
return;
}

linked_list_node* node;
linked_list_node* next;

for (node = list->head; node != NULL; node = next) {
next = node->next;
DEALLOCATE(node);
}

list->head = NULL;
list->tail = NULL;
list->size = 0;
DEALLOCATE(list);
}

void delete_single_match_linked_list(linked_list* list, int target) {
if (list == NULL || list->head == NULL) {
return;
Expand All @@ -114,7 +125,7 @@ void delete_single_match_linked_list(linked_list* list, int target) {

if (node->value == target) {
if (list->size == 1) {
delete_linked_list(list);
deallocate_linked_list(list);
return;
}

Expand Down Expand Up @@ -162,12 +173,13 @@ void reverse_linked_list(linked_list*const list) {
list->tail = current;
}

// TODO: Return an empty list when list is not NULL but its contents are.
linked_list* copy_linked_list(const linked_list*const list) {
if (list == NULL || list->head == NULL) {
return NULL;
}

linked_list* copy = new_linked_list(list->head->value);
linked_list* copy = allocate_linked_list(list->head->value);
linked_list_node* node = list->head->next;

while (node != NULL) {
Expand Down Expand Up @@ -205,7 +217,7 @@ bool are_equal_linked_list(

inline bool is_empty_linked_list(const linked_list*const list) {
return list == NULL
|| list->head == NULL
|| list->tail == NULL
|| list->size == 0;
|| (list->head == NULL
&& list->tail == NULL
&& list->size == 0);
}
Loading

0 comments on commit 46a923a

Please sign in to comment.