ccousas

estruturas de datos e algoritmos en C
Log | Files | Refs

commit 8ec22d2f462b18812f82b9b2e6f33e23a1a4bab4
Author: x0tero <gxoelotero@gmail.com>
Date:   Tue,  5 Mar 2024 11:06:46 +0100

lista dinamica de nodos en C

Diffstat:
Alista_nodos/Makefile | 14++++++++++++++
Alista_nodos/lista_nodos.c | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alista_nodos/lista_nodos.h | 27+++++++++++++++++++++++++++
Alista_nodos/main.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 218 insertions(+), 0 deletions(-)

diff --git a/lista_nodos/Makefile b/lista_nodos/Makefile @@ -0,0 +1,14 @@ +EXEC := test +CFLAGS := -Wall +LDFLAGS := +LIBS := +CC := gcc +SRCS := lista_nodos.c main.c +OBX := $(SRCS:.c=.o) +all: $(EXEC) +$(EXEC): ${OBX} + $(CC) $(LDFLAGS) $^ -o $@ $(LIBS) +main.o: main.c lista_nodos.o + $(CC) $(CFLAGS) $< -c -o $@ +clean: + rm -f $(EXEC) $(OBX) diff --git a/lista_nodos/lista_nodos.c b/lista_nodos/lista_nodos.c @@ -0,0 +1,111 @@ +#include <stdlib.h> +#include "lista_nodos.h" + + +NODE_LIST_T* init_node_list() { + NODE_LIST_T* list = malloc(sizeof(NODE_LIST_T)); + list->head = NULL; + list->tail = NULL; + list->length = 0; + return list; +} + + +int node_list_append(void* item, NODE_LIST_T* list) { + NODE_T* node = malloc(sizeof(NODE_T)); + node->item = item; + node->sig = NULL; + if (list->head == NULL) list->head = node; + else list->tail->sig = node; + + list->tail = node; + list->length++; + return 1; +} + + +// Devolve NULL se o indice e maior que o tamanho da lista +// ou se a lista e baleira, de outra forma devolve void* item +void* node_list_get(int index, NODE_LIST_T* list) { + if (list->head == NULL) return NULL; + else{ + NODE_T* tmp = list->head; + for(int i = 0; i < index; i++){ + if(tmp->sig == NULL) return NULL; + else tmp = tmp->sig; + } + return tmp->item; + } +} + + +void node_list_print(NODE_LIST_T* list, void (*print_method)(void* x, int y)) { + NODE_T* tmp = list->head; + for(int i = 0; i < list->length; i++){ + print_method(tmp->item, i); + tmp = tmp->sig; + } +} + + +// Devolve o indice do obxeto ou -1 senon esta +int node_list_find(NODE_LIST_T* list, void* item, int (*equals_method)(void* x, void* y)) { + NODE_T* tmp = list->head; + for(int i = 0; i < list->length; i++){ + if (equals_method(tmp->item, item)){ + return i; + } + tmp = tmp->sig; + } + return -1; +} + + +// Elimina un elemento dada a posicion na lista (devolve 0 se non se puido eliminar, 1 se si se puido) +int node_list_delete(int index, NODE_LIST_T* list, void (*free_method)(void* x)) { + if (list->head == NULL) return 0; + else if (list->head == list->tail) { + // Deixar a lista baleira + if (free_method) free_method(list->head->item); + free(list->head); + list->head = NULL; + list->tail = NULL; + } + else if (index == 0) { + NODE_T* tmp = list->head; + list->head = list->head->sig; + if (free_method) free_method(tmp->item); + free(tmp); + } + else{ + NODE_T** tmp = &list->head; + NODE_T* aux; + for(int i = 0; i < index; i++){ + if((*tmp)->sig == NULL) return 0; + else { + tmp = &((*tmp)->sig); + } + } + aux = *tmp; + *tmp = (*tmp)->sig; + + free_method(aux->item); + free(aux); + } + list->length--; + return 1; +} + + +void node_list_free(NODE_LIST_T* list, void(*free_method)(void* x)) { + if (list->head == NULL) return; + NODE_T* tmp = list->head; + NODE_T* aux; + for(int i = 0; i < list->length; i++){ + aux = tmp; + tmp = tmp->sig; + free_method(aux->item); + free(aux); + } + free(list); +} diff --git a/lista_nodos/lista_nodos.h b/lista_nodos/lista_nodos.h @@ -0,0 +1,27 @@ +#ifndef NODE_LIST_H +#define NODE_LIST_H + +// Typedef declaration + +typedef struct NODE{ + void* item; + struct NODE* sig; +} NODE_T; + +typedef struct NODE_LIST{ + NODE_T* head; + NODE_T* tail; + int length; +} NODE_LIST_T; + +// Function declaration + +NODE_LIST_T* init_node_list(); +int node_list_append(void* item, NODE_LIST_T* list); +void* node_list_get(int index, NODE_LIST_T* list); +void node_list_print(NODE_LIST_T* list, void (*print_method)(void* x, int y)); +int node_list_find(NODE_LIST_T* list, void* item, int (*equals_method)(void* x, void* y)); +int node_list_delete(int index, NODE_LIST_T* list, void (*free_method)(void* x)); +void node_list_free(NODE_LIST_T* list, void(*free_method)(void* x)); + +#endif diff --git a/lista_nodos/main.c b/lista_nodos/main.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "lista_nodos.h" + + +struct TEST_STRUCT{ + int id; + char* name; +}; + +struct TEST_STRUCT* init_test_struct(const char* name){ + struct TEST_STRUCT* test_struct = calloc(1, sizeof(struct TEST_STRUCT)); + test_struct->name = calloc(strlen(name)+1, sizeof(char) ); + strcpy(test_struct->name, name); + test_struct->id = 1; + return test_struct; +} + + +void test_free(void* x){ + struct TEST_STRUCT* test = (struct TEST_STRUCT*) x; + free(test->name); + free(test); +} + + +// 1: Son iguais, 0: Son diferentes +int test_equals(void* x, void* y){ + if ( !strcmp(((struct TEST_STRUCT*) x)->name, ((struct TEST_STRUCT*) y)->name) ){ + if( ((struct TEST_STRUCT*) x)->id == ((struct TEST_STRUCT*) y)->id ) + return 1; + } + return 0; +} + +void test_print(void* item, int index){ + printf("Elemento %d: %s\n", index, ((struct TEST_STRUCT*)(item))->name ); +} + + +int main(){ + NODE_LIST_T* list = init_node_list(); + struct TEST_STRUCT* item1 = init_test_struct("test1"); + + printf("list size: %d\n", list->length); + + node_list_append(item1, list); + node_list_append(init_test_struct("test2"), list); + node_list_append(init_test_struct("test3"), list); + node_list_append(init_test_struct("test4"), list); + node_list_append(init_test_struct("test5"), list); + node_list_append(init_test_struct("test6"), list); + + node_list_delete(2, list, test_free); + + node_list_print(list, test_print); + printf("list size: %d\n", list->length); + + int indexf = node_list_find(list, init_test_struct("test6"), test_equals); + printf("Index found: %d", indexf); + + node_list_free(list, test_free); + return 0; + +}