diff --git a/genericLL/Makefile b/genericLL/Makefile new file mode 100644 index 0000000..b0b316d --- /dev/null +++ b/genericLL/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS = -Wall +LDFLAGS = +OBJFILES = primitive_types_functions.o genericLL.o main.o +TARGET = main + +all: $(TARGET) + +$(TARGET): $(OBJFILES) + $(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS) +clean: + rm -f $(OBJFILES) $(TARGET) *~ \ No newline at end of file diff --git a/genericLL/d b/genericLL/d new file mode 100644 index 0000000..e69de29 diff --git a/genericLL/genericLL.c b/genericLL/genericLL.c new file mode 100644 index 0000000..8cd3daa --- /dev/null +++ b/genericLL/genericLL.c @@ -0,0 +1,77 @@ +#include +#include +#include "genericLL.h" + +void push(Node **head, void *val, size_t data_size) +{ + Node *new_node = (Node *)malloc(sizeof(Node)); + + new_node->data = malloc(data_size); + /*como char tem um byte copia byte por byte*/ + for (int i = 0; i < data_size; i++) + *(char *)(new_node->data + i) = *(char *)(val + i); + + new_node->next = NULL; + if (*head == NULL) + { + *head = new_node; + return; + } + Node *p = *head; + while (p->next != NULL) + p = p->next; + + p->next = new_node; + return; +} + +/*Como a lista é genérica, ou seja, pode ter qualquer tipo de data type, passamos uma função específica para o data type que está sendo usado +para comparar os elementos.*/ +void delete (Node **head, void *val, int (*compare)(void *, void *)) +{ + if (*head == NULL) + return; + + Node *p = *head; + Node *ant = NULL; + + if (p->next == NULL && (*compare)(p->data, val) == 0) + { + *head = NULL; + return; + } + + while (p->next != NULL) + { + int comp = (*compare)(p->data, val); + if (comp == 0) + { + + if (ant == NULL) + { + *head = p->next; + free(p->data); + return; + } + else + { + ant->next = p->next; + free(p->data); + } + } + ant = p; + p = p->next; + } +} +/*Essa função vai apenas percorrer a lista e passar cada elemento para uma função +recebida como parâmetro que printa especificamente aquele data type. */ +void printList(Node *head, void (*printData)(void *)) +{ + Node *p = head; + while (p) + { + (*printData)(p->data); + p = p->next; + } + puts(""); +} diff --git a/genericLL/genericLL.h b/genericLL/genericLL.h new file mode 100644 index 0000000..1ea527e --- /dev/null +++ b/genericLL/genericLL.h @@ -0,0 +1,9 @@ +typedef struct Node +{ + void *data; + struct Node *next; +} Node; + +void push(Node **head, void *val, size_t size); +void delete (Node **head, void *val, int (*compare)(void *, void *)); +void printList(Node *head, void (*printData)(void *)); \ No newline at end of file diff --git a/genericLL/main.c b/genericLL/main.c new file mode 100644 index 0000000..8eb23f4 --- /dev/null +++ b/genericLL/main.c @@ -0,0 +1,62 @@ +#include +#include +#include "genericLL.h" + +/*print e compare para char, int, float, double and string*/ +#include "primitive_types_functions.h" + +int main() +{ + Node *integers = NULL; + Node *floats = NULL; + Node *chars = NULL; + Node *strings = NULL; + + char char_arguments[10] = {97, 98, 99, 100, 101, 102, 103, 104, 105, 106}; + for (int i = 0; i < 10; i++) + push(&chars, &char_arguments[i], sizeof(char_arguments[i])); + printList(chars, printChar); + for (int i = 0; i < 10; i++) + { + printf("Após tirar o %c: ", char_arguments[i]); + delete (&chars, &char_arguments[i], compareChar); + printList(chars, printChar); + } + + int arguments[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; + for (int i = 0; i < 10; i++) + push(&integers, &arguments[i], sizeof(arguments[i])); + printList(integers, printInt); + for (int i = 0; i < 10; i++) + { + printf("Após tirar o %d: ", arguments[i]); + delete (&integers, &arguments[i], compareInt); + printList(integers, printInt); + } + + float float_arguments[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; + for (int i = 0; i < 10; i++) + push(&floats, &float_arguments[i], sizeof(float_arguments[i])); + printList(floats, printFloat); + for (int i = 0; i < 10; i++) + { + printf("Após tirar o %f: ", float_arguments[i]); + delete (&floats, &float_arguments[i], compareFloat); + printList(floats, printFloat); + } + + char strings_arguments[10][20] = + { + "Casa", "cavalo", "carteira", "fidedigno", "nasogástrico", + "hipopótamo", "malabarista", "JBL", "React-native", "Swift"}; + + for (int i = 0; i < 10; i++) + push(&strings, &strings_arguments[i], sizeof(strings_arguments[i])); + printList(strings, printString); + for (int i = 0; i < 10; i++) + { + printf("Após tirar o %s: ", strings_arguments[i]); + delete (&strings, &strings_arguments[i], compareStrings); + printList(strings, printString); + } +} \ No newline at end of file diff --git a/genericLL/primitive_types_functions.c b/genericLL/primitive_types_functions.c new file mode 100644 index 0000000..e787a33 --- /dev/null +++ b/genericLL/primitive_types_functions.c @@ -0,0 +1,84 @@ +#include +#include +#include "primitive_types_functions.h" + +void printChar(void *data) +{ + printf("%c ", *(char *)data); +} + +void printInt(void *data) +{ + printf("%d ", *(int *)data); +} + +void printFloat(void *data) +{ + printf("%f ", *(float *)data); +} + +void printDouble(void *data) +{ + printf("%lf ", *(double *)data); +} + +void printString(void *data) +{ + printf("%s ", (char *)data); +} + +int compareChar(void *a, void *b) +{ + char num1 = *(char *)a; + char num2 = *(char *)b; + if (num1 > num2) + return 1; + if (num1 == num2) + return 0; + return -1; +} + +int compareInt(void *a, void *b) +{ + int num1 = *(int *)a; + int num2 = *(int *)b; + if (num1 > num2) + return 1; + if (num1 == num2) + return 0; + return -1; +} + +int compareFloat(void *a, void *b) +{ + float precision = 0.000001; + float num1 = *(float *)a; + float num2 = *(float *)b; + + float aux = num1 - num2; + if (aux > precision) + return 1; + if (aux <= precision && aux > precision * -1.0) + return 0; + return -1; +} + +int compareDouble(void *a, void *b) +{ + double precision = 0.000001; + double num1 = *(double *)a; + double num2 = *(double *)b; + + double aux = num1 - num2; + if (aux > precision) + return 1; + if (aux <= precision && aux > precision * -1.0) + return 0; + return -1; +} + +int compareStrings(void *a, void *b) +{ + + return (strcmp((char *)a, (char *)b)); +} \ No newline at end of file diff --git a/genericLL/primitive_types_functions.h b/genericLL/primitive_types_functions.h new file mode 100644 index 0000000..65568b0 --- /dev/null +++ b/genericLL/primitive_types_functions.h @@ -0,0 +1,21 @@ + + +void printChar(void *data); + +void printInt(void *data); + +void printFloat(void *data); + +void printDouble(void *data); + +void printString(void *data); + +int compareChar(void *a, void *b); + +int compareInt(void *a, void *b); + +int compareFloat(void *a, void *b); + +int compareDouble(void *a, void *b); + +int compareStrings(void *a, void *b); \ No newline at end of file