Skip to content

Commit

Permalink
Merge pull request #28 from urielgarciarivas/main
Browse files Browse the repository at this point in the history
a
  • Loading branch information
urielgarciarivas authored May 12, 2024
2 parents 05af01e + af6ccfa commit c6c0d23
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
10 changes: 7 additions & 3 deletions DataStructures/inc/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ extern void add_to_linked_list(linked_list*const list, int value);

extern void delete_linked_list(linked_list* list);

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

extern void erase_all_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.
extern void reverse_linked_list(linked_list*const list);
Expand Down
36 changes: 13 additions & 23 deletions DataStructures/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,28 @@

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
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: test
test: array_test hash_map_test hash_set_test linked_list_test sort_map_test sort_set_test
( ./array_test && ./hash_map_test && ./hash_set_test && ./linked_list_test \
&& ./sort_map_test && ./sort_set_test ) ; make clean

test: $(TESTS)
for TEST in $(TESTS); do \
./$$TEST; \
done; \
make clean

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

array_test: test/array_test.c $(OBJ)
$(CC) $(CFLAGS) $(INC) $< -o $@ $(OBJ)

hash_map_test: test/hash_map_test.c $(OBJ)
$(CC) $(CFLAGS) $(INC) $< -o $@ $(OBJ)

hash_set_test: test/hash_set_test.c $(OBJ)
$(CC) $(CFLAGS) $(INC) $< -o $@ $(OBJ)

linked_list_test: test/linked_list_test.c $(OBJ)
$(CC) $(CFLAGS) $(INC) $< -o $@ $(OBJ)

sort_map_test: test/sort_map_test.c $(OBJ)
$(CC) $(CFLAGS) $(INC) $< -o $@ $(OBJ)

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

clean:
rm array_test hash_map_test hash_set_test linked_list_test sort_map_test \
sort_set_test $(OBJ)
rm $(TESTS) $(OBJ)

# $(wildcard *.o)
23 changes: 23 additions & 0 deletions DataStructures/src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,27 @@
*/

#include "../inc/array.h"
#include "../inc/memory.h"

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

ALLOCATE(array, response);
ALLOCATE(int, response->data);
response->size = 1;
*(response->data) = value;

return response;
}

inline void delete_array(array* arr) {
if (arr == NULL) {
return;
}

if (arr->data != NULL) {
DEALLOCATE(arr->data);
}

DEALLOCATE(arr);
}
4 changes: 2 additions & 2 deletions DataStructures/src/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ inline void add_to_linked_list(linked_list*const list, int value) {
}

void delete_linked_list(linked_list* list) {
if (list == NULL || list->head == NULL) {
if (list == NULL) {
return;
}

Expand All @@ -105,7 +105,7 @@ void delete_linked_list(linked_list* list) {
DEALLOCATE(list);
}

void erase_single_match_linked_list(linked_list* list, int target) {
void delete_single_match_linked_list(linked_list* list, int target) {
if (list == NULL || list->head == NULL) {
return;
}
Expand Down
16 changes: 7 additions & 9 deletions DataStructures/test/array_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@
int main(void) {
START_TEST("array_test");

EXPECT_TRUE(1);
/*
array arr = new_array();
const size_t length = 1;
const int first_element = length - 1;
array* arr = new_array(first_element);

delete_array(arr);
// len should not be >= INT_MAX.
const size_t len = 5;
const int last_element = len - 1;
const int first_element = 0;
linked_list* list = new_linked_list(first_element);
EXPECT_EQUAL(arr->data[0], first_element);
EXPECT_EQUAL(length, arr->size);

delete_array(arr);
/*
for (int i = first_element + 1; i <= last_element; ++i) {
add_to_linked_list(list, i);
}
Expand Down
10 changes: 5 additions & 5 deletions DataStructures/test/linked_list_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
int main(void) {
START_TEST("linked_list_test");

// len should not be >= INT_MAX.
const size_t len = 5;
const int last_element = len - 1;
// length should not be >= INT_MAX.
const size_t length = 5;
const int last_element = length - 1;
const int first_element = 0;
linked_list* list = new_linked_list(first_element);

Expand All @@ -34,7 +34,7 @@ int main(void) {

EXPECT_TRUE(exist_in_linked_list(list, first_element + 2));
EXPECT_FALSE(exist_in_linked_list(list, last_element + 1));
EXPECT_EQUAL(list->size, len);
EXPECT_EQUAL(list->size, length);

linked_list* list_copy = copy_linked_list(list);

Expand All @@ -46,7 +46,7 @@ int main(void) {
EXPECT_TRUE(are_equal_linked_list(list, list_copy));
EXPECT_TRUE(are_equal_linked_list(list_copy, list_copy));

erase_single_match_linked_list(list_copy, 3);
delete_single_match_linked_list(list_copy, 3);
EXPECT_FALSE(exist_in_linked_list(list_copy, 3));
EXPECT_FALSE(are_equal_linked_list(list, list_copy));

Expand Down

0 comments on commit c6c0d23

Please sign in to comment.