Skip to content

Commit

Permalink
Merge pull request #19 from Garz4/main
Browse files Browse the repository at this point in the history
a
  • Loading branch information
urielgarciarivas authored Dec 1, 2023
2 parents 20d008f + 8dd39af commit a582981
Show file tree
Hide file tree
Showing 38 changed files with 489 additions and 320 deletions.
28 changes: 17 additions & 11 deletions DataStructures/Makefile → DataStructures/makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2022 Uriel Rivas
# Copyright (c) 2024 Uriel Rivas
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -14,28 +14,34 @@
#
# https://github.com/zoningorg/zoning/blob/main/LICENSE

all: test
./linked_list_test ; make clean
CC = gcc
CFLAGS = -Wall -Werror -Wpedantic

all: test
test: hash_map_test hash_set_test linked_list_test sort_map_test sort_set_test
(./hash_map_test && ./hash_set_test && ./linked_list_test && ./sort_map_test && ./sort_set_test) ; make clean
( ./hash_map_test && ./hash_set_test && ./linked_list_test \
&& ./sort_map_test && ./sort_set_test ) ; make clean

linked_list_test: test/linked_list_test.c linked_list.o
$(CC) $(CFLAGS) test/linked_list_test.c -o linked_list_test src/linked_list.o

linked_list_test: test/linked_list_test.c
gcc test/linked_list_test.c -o linked_list_test
linked_list.o: src/linked_list.c src/linked_list.h
$(CC) $(CFLAGS) -c src/linked_list.c -o src/linked_list.o

hash_map_test: test/hash_map_test.c
gcc test/hash_map_test.c -o hash_map_test
$(CC) $(CFLAGS) test/hash_map_test.c -o hash_map_test

hash_set_test: test/hash_set_test.c
gcc test/hash_set_test.c -o hash_set_test
$(CC) $(CFLAGS) test/hash_set_test.c -o hash_set_test

sort_map_test: test/sort_map_test.c
gcc test/sort_map_test.c -o sort_map_test
$(CC) $(CFLAGS) test/sort_map_test.c -o sort_map_test

sort_set_test: test/sort_set_test.c
gcc test/sort_set_test.c -o sort_set_test
$(CC) $(CFLAGS) test/sort_set_test.c -o sort_set_test

clean:
rm linked_list_test hash_map_test hash_set_test sort_map_test sort_set_test
rm linked_list_test hash_map_test hash_set_test sort_map_test \
sort_set_test src/linked_list.o

# $(wildcard *.o)
26 changes: 26 additions & 0 deletions DataStructures/src/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* MIT License
*
* Copyright (c) 2024 Uriel Rivas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* https://github.com/zoningorg/zoning/blob/main/LICENSE
*/

#ifndef __ZNG_ARRAY_H__
#define __ZNG_ARRAY_H__

struct array {

};

#endif // __ZNG_ARRAY_H__
8 changes: 4 additions & 4 deletions DataStructures/src/hash_map.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Uriel Rivas
* Copyright (c) 2024 Uriel Rivas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,7 +16,7 @@
* https://github.com/zoningorg/zoning/blob/main/LICENSE
*/

#ifndef HASH_MAP_H_
#define HASH_MAP_H_
#ifndef __ZNG_HASH_MAP_H__
#define __ZNG_HASH_MAP_H__

#endif // HASH_MAP_H_
#endif // __ZNG_HASH_MAP_H__
8 changes: 4 additions & 4 deletions DataStructures/src/hash_set.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Uriel Rivas
* Copyright (c) 2024 Uriel Rivas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,7 +16,7 @@
* https://github.com/zoningorg/zoning/blob/main/LICENSE
*/

#ifndef HASH_SET_H_
#define HASH_SET_H_
#ifndef __ZNG_HASH_SET_H__
#define __ZNG_HASH_SET_H__

#endif // HASH_SET_H_
#endif // __ZNG_HASH_SET_H__
216 changes: 216 additions & 0 deletions DataStructures/src/linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* MIT License
*
* Copyright (c) 2024 Uriel Rivas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* https://github.com/zoningorg/zoning/blob/main/LICENSE
*/

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

#include "linked_list.h"
#include "memory.h"

// Every new linked list needs to be deleted using delete_linked_list(...).
linked_list*
new_linked_list(int value) {
linked_list* response;

ALLOCATE(linked_list, response);
ALLOCATE(linked_list_node, response->head);
response->size = 1;
response->head->value = value;
response->head->next = NULL;
response->tail = response->head;

return response;
}

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

while (head != NULL) {
printf("%d", head->value);
head = head->next;

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

printf("};\n");
}

// Linear time, stops when finding the first occurence.
bool
exist_in_linked_list(const linked_list* list, int value) {
linked_list_node* head = list->head;

while (head != NULL) {
if (head->value == value) {
return true;
}

head = head->next;
}

return false;
}

// TODO(Garz4): Fix edge case when list is not NULL, but its head and tail are.
void
add_to_linked_list(linked_list* list, int value) {
if (list == NULL) {
return;
}

ALLOCATE(linked_list_node, list->tail->next);
list->tail = list->tail->next;
list->tail->value = value;
list->tail->next = NULL;
list->size++;
}

void
delete_linked_list(linked_list* list) {
if (list == NULL || list->head == 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);
}

// Erases first occurence of 'target' in the linked list.
// It frees its memory.
void
erase_single_match_linked_list(linked_list* list, int target) {
if (list == NULL || list->head == NULL) {
return;
}

linked_list_node* node = list->head;

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

list->head = node->next;
DEALLOCATE(node);
list->size--;
return;
}

while (node->next != NULL) {
if (node->next->value == target) {
if (node->next == list->tail) {
list->tail = node;
}

DEALLOCATE(node->next);
list->size--;
return;
}

node = node->next;
}
}

void
erase_all_match_linked_list(linked_list* list, int target) {

}

// Linear time.
void
reverse_linked_list(linked_list* list) {
if (list == NULL) {
return;
}

linked_list_node* current = list->head;
linked_list_node* previous = NULL;
linked_list_node* next = NULL;

while (current != NULL) {
next = current->next;
current->next = previous;
previous = current;
current = next;
}

current = list->head;
list->head = list->tail;
list->tail = current;
}

// By making a copy, deleting that copy is also necessary.
linked_list*
copy_linked_list(const linked_list* list) {
if (list == NULL || list->head == NULL) {
return NULL;
}

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

while (node != NULL) {
add_to_linked_list(copy, node->value);
node = node->next;
}

return copy;
}

bool
equal_linked_list(const linked_list* lhs, const linked_list* rhs) {
if (lhs == NULL && rhs == NULL) {
return true;
} else if (lhs == NULL || rhs == NULL || lhs->size != rhs->size) {
return false;
}

linked_list_node* lhs_node = lhs->head;
linked_list_node* rhs_node = rhs->head;

while (lhs_node != NULL && rhs_node != NULL) {
if (lhs_node->value != rhs_node->value) {
return false;
}

lhs_node = lhs_node->next;
rhs_node = rhs_node->next;
}

return lhs_node == NULL && rhs_node == NULL;
}

void
sort_linked_list(linked_list* list) {}
Loading

0 comments on commit a582981

Please sign in to comment.