Skip to content

Commit

Permalink
Merge pull request #31 from urielgarciarivas/main
Browse files Browse the repository at this point in the history
a
  • Loading branch information
urielgarciarivas authored May 17, 2024
2 parents 46a923a + e73237b commit 2124607
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 101 deletions.
7 changes: 5 additions & 2 deletions DataStructures/inc/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ typedef struct __zng_array {
extern array* allocate_array(int value);
extern array* allocate_empty_array();
extern void deallocate_array(array* arr);
// TODO:
extern void deallocate_elements_in_array(array* arr);

extern bool is_empty_array(const array*const arr);

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

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__
19 changes: 11 additions & 8 deletions DataStructures/inc/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,25 @@ typedef struct __zng_ll {
extern linked_list* allocate_linked_list(int value);
extern linked_list* allocate_empty_linked_list();
extern void deallocate_linked_list(linked_list* list);
// TODO:
extern void deallocate_elements_in_linked_list(linked_list* list);

// Example output to screen: "list = {1, 2, 3, 4, 5};"
extern bool is_empty_linked_list(const linked_list*const 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 add_head_linked_list(linked_list*const list, int value);
extern void add_tail_linked_list(linked_list*const list, int value);
extern void add_to_empty_linked_list(linked_list*const list, int value);
// TODO: Finish head and tail functions.
extern void add_as_head_linked_list(linked_list*const list, int value);
extern void add_at_index_linked_list(
linked_list*const list, int value, int index);

// Deletes first occurence of 'target' in the linked list.
// It frees its memory.
// 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);
Expand All @@ -67,6 +72,4 @@ extern bool are_equal_linked_list(

extern void sort_linked_list(linked_list*const list);

extern bool is_empty_linked_list(const linked_list*const list);

#endif // __ZNG_LINKED_LIST_H__
47 changes: 34 additions & 13 deletions DataStructures/inc/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,43 @@
#ifndef __ZNG_MEMORY_H__
#define __ZNG_MEMORY_H__

#include <stdio.h>
#include <stdlib.h>

#include "../../Terminal/outputstream.h"
#define ALLOCATE(type, pointer) \
do { \
(pointer) = (type *) malloc(sizeof(type)); \
if ((pointer) == NULL) { \
fprintf(stderr, "ERROR: Out of memory. " \
"Could not allocate memory of type '" \
#type \
"' for the variable '" \
#pointer \
"' using malloc.\n"); \
} \
} while (0)

#define ALLOCATE(type, pointer) \
do { \
(pointer) = (type *) malloc(sizeof(type)); \
if ((pointer) == NULL) { \
STDERR_RED("ERROR: Out of memory.\n"); \
} \
} while (0);
#define REALLOCATE(type, pointer, size) \
do { \
type* __zng_temp_ptr = (type *) realloc((pointer), (size) * sizeof(type)); \
if (__zng_temp_ptr == NULL) { \
fprintf(stderr, "ERROR: Out of memory. " \
"Could not allocate memory of type '" \
#type \
"' for the variable '" \
#pointer \
"' that required a new size of '" \
"%zu" \
"' using malloc.\n", (size)); \
} else { \
pointer = __zng_temp_ptr; \
} \
} while (0)

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

#endif // __ZNG_MEMORY_H__
7 changes: 5 additions & 2 deletions DataStructures/inc/sort_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ typedef struct __zng_ss {
extern sort_set* allocate_sort_set(int value);
extern sort_set* allocate_empty_sort_set();
extern void deallocate_sort_set(sort_set* list);
// TODO:
extern void deallocate_elements_in_sort_set(sort_set* list);

extern bool is_empty_sort_set(const sort_set*const set);

extern void add_to_sort_set(sort_set*const set, int value);
extern void add_to_empty_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 bool is_empty_sort_set(const sort_set*const set);

#endif // __ZNG_SORT_SET_H__
35 changes: 31 additions & 4 deletions DataStructures/src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,38 @@ inline void deallocate_array(array* arr) {
DEALLOCATE(arr);
}

inline bool is_empty_array(const array*const arr) {
return arr == NULL || (arr->data == NULL && arr->size == 0);
}

inline void add_to_array(array*const arr, int value) {
if (arr == NULL) {
return;
} else if (is_empty_array(arr)) {
add_to_empty_array(arr, value);
return;
}

arr->size++;
REALLOCATE(int, arr->data, arr->size);
arr->data[arr->size - 1] = value;
}

inline void add_to_empty_array(array*const arr, int value) {
if (arr == NULL) {
return;
}

ALLOCATE(int, arr->data);
arr->size = 1;
*(arr->data) = value;
}

bool exist_in_array(const array*const arr, int target) {
if (is_empty_array(arr)) {
return false;
}

for (size_t i = 0; i < arr->size; ++i) {
if (arr->data[i] == target) {
return true;
Expand All @@ -61,7 +92,3 @@ bool exist_in_array(const array*const arr, int target) {

return false;
}

inline bool is_empty_array(const array*const arr) {
return arr == NULL || (arr->data == NULL && arr->size == 0);
}
38 changes: 21 additions & 17 deletions DataStructures/src/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ void deallocate_linked_list(linked_list* list) {
DEALLOCATE(list);
}

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

void print_linked_list(const linked_list*const list) {
if (list == NULL) {
printf("list = {};\n");
if (is_empty_linked_list(list)) {
printf("list = {}\n");
return;
}

Expand All @@ -78,15 +85,15 @@ void print_linked_list(const linked_list*const list) {
head = head->next;

if (head != NULL) {
printf(", ");
printf("}->{");
}
}

printf("};\n");
printf("}\n");
}

bool exist_in_linked_list(const linked_list*const list, int value) {
if (list == NULL) {
if (is_empty_linked_list(list)) {
return false;
}

Expand Down Expand Up @@ -117,7 +124,7 @@ inline void add_to_linked_list(linked_list*const list, int value) {
}

void delete_single_match_linked_list(linked_list* list, int target) {
if (list == NULL || list->head == NULL) {
if (is_empty_linked_list(list)) {
return;
}

Expand All @@ -141,7 +148,10 @@ void delete_single_match_linked_list(linked_list* list, int target) {
list->tail = node;
}

DEALLOCATE(node->next);
linked_list_node* aux;
aux = node->next;
node->next = node->next->next;
DEALLOCATE(aux);
list->size--;
return;
}
Expand All @@ -153,7 +163,7 @@ void delete_single_match_linked_list(linked_list* list, int target) {
//void erase_all_match_linked_list(linked_list* list, int target) {}

void reverse_linked_list(linked_list*const list) {
if (list == NULL) {
if (is_empty_linked_list(list)) {
return;
}

Expand All @@ -173,10 +183,11 @@ 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) {
if (list == NULL) {
return NULL;
} else if (is_empty_linked_list(list)) {
return allocate_empty_linked_list();
}

linked_list* copy = allocate_linked_list(list->head->value);
Expand Down Expand Up @@ -214,10 +225,3 @@ bool are_equal_linked_list(
}

//void sort_linked_list(linked_list*const list) {}

inline bool is_empty_linked_list(const linked_list*const list) {
return list == NULL
|| (list->head == NULL
&& list->tail == NULL
&& list->size == 0);
}
14 changes: 7 additions & 7 deletions DataStructures/src/sort_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ void deallocate_sort_set(sort_set* set) {
DEALLOCATE(set);
}

inline bool is_empty_sort_set(const sort_set*const set) {
return set == NULL
|| (set->root == NULL
&& set->size == 0);
}

//inline void add_to_sort_set(sort_set*const set, int value) { set; }

bool exist_in_sort_set(const sort_set*const set, int target) {
if (set == NULL || set->size == 0) {
if (is_empty_sort_set(set)) {
return false;
}

Expand All @@ -77,9 +83,3 @@ bool exist_in_sort_set(const sort_set*const set, int target) {

return false;
}

inline bool is_empty_sort_set(const sort_set*const set) {
return set == NULL ||
(set->root == NULL &&
set->size == 0);
}
7 changes: 6 additions & 1 deletion DataStructures/test/src/array_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ int main(void) {
array* arr = allocate_array(first_element);

EXPECT_EQUAL(arr->data[0], first_element);
EXPECT_EQUAL(length, arr->size);
EXPECT_EQUAL(arr->size, length);

add_to_array(arr, 5);

EXPECT_EQUAL(arr->data[1], 5);
EXPECT_EQUAL(arr->size, 2);

deallocate_array(arr);
/*
Expand Down
3 changes: 3 additions & 0 deletions DataStructures/test/src/linked_list_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ int main(void) {
linked_list_node* node;
int curr_element = last_element;

print_linked_list(list);
print_linked_list(list_copy);

for (node = list->head; node != NULL; node = node->next) {
EXPECT_EQUAL(node->value, curr_element--);
}
Expand Down
14 changes: 7 additions & 7 deletions Terminal/outputstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@
* https://github.com/zoningorg/zoning/blob/main/LICENSE
*/

#ifndef OUTPUTSTREAM_H_
#define OUTPUTSTREAM_H_
#ifndef __ZNG_OUTPUTSTREAM_H__
#define __ZNG_OUTPUTSTREAM_H__

#include <stdio.h>
#include <stdlib.h>

#define EXIT(code) exit(code);

// To make it bold, add 01; between the brace and the number

#define STDERR_RED(...) \
do { \
fprintf(stderr, "\033[91m"); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\033[0m"); \
} while (0);
fflush(stderr); \
} while (0)

#define STDOUT_GREEN(...) \
do { \
fprintf(stdout, "\033[92m"); \
fprintf(stdout, __VA_ARGS__); \
fprintf(stdout, "\033[0m"); \
} while (0);
fflush(stderr); \
} while (0)

#endif // OUTPUTSTREAM_H_
#endif // __ZNG_OUTPUTSTREAM_H__
Loading

0 comments on commit 2124607

Please sign in to comment.