ccousas

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

commit a77dbac157dfeccf7bf19c1492656999dcb8bc98
parent 8ec22d2f462b18812f82b9b2e6f33e23a1a4bab4
Author: x0tero <gxoelotero@gmail.com>
Date:   Tue,  5 Mar 2024 11:26:35 +0100

lista dinamica baseada en array

Diffstat:
Alista_dinamica/Makefile | 16++++++++++++++++
Alista_dinamica/lista_dinamica.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alista_dinamica/lista_dinamica.h | 17+++++++++++++++++
Alista_dinamica/lista_dinamica.o | 0
Alista_dinamica/main.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alista_dinamica/main.o | 0
Alista_dinamica/test | 0
7 files changed, 182 insertions(+), 0 deletions(-)

diff --git a/lista_dinamica/Makefile b/lista_dinamica/Makefile @@ -0,0 +1,16 @@ +EXEC := test +CFLAGS := -Wall +LDFLAGS := +LIBS := +CC := gcc +SRCS := lista_dinamica.c main.c +OBX := $(SRCS:.c=.o) +all: $(EXEC) +$(EXEC): ${OBX} + $(CC) $(LDFLAGS) $^ -o $@ $(LIBS) +lista_dinamica.o: lista_dinamica.c + $(CC) $(CFLAGS) $< -c -o $@ +main.o: main.c lista_dinamica.o + $(CC) $(CFLAGS) $< -c -o $@ +clean: + rm -f $(EXEC) $(OBX) diff --git a/lista_dinamica/lista_dinamica.c b/lista_dinamica/lista_dinamica.c @@ -0,0 +1,88 @@ +#include <stdlib.h> +#include "lista_dinamica.h" + + +dynamic_list_T* init_dynamic_list(size_t item_size){ + dynamic_list_T* list = calloc(1, sizeof(dynamic_list_T)); + list->item_size = item_size; + list->length = 0; + list->items = (void*) 0; + + return list; +} + + +// Devolve o indice dun elemento se esta na lista e -1 senon esta na lista +int dynamic_list_find(dynamic_list_T* dynamic_list, void* item, int (*equals_method)(void* x, void* y)){ + for(int i = 0; i < dynamic_list->length; i++){ + if(equals_method(dynamic_list->items[i], item)){ + return i; + } + } + return -1; +} + + +void dynamic_list_append(dynamic_list_T* dynamic_list, void* item){ + dynamic_list->length += 1; + if (dynamic_list->items == (void*) 0){ // Se a lista esta baleira reserva memoria para un so elemento + dynamic_list->items = calloc(1, dynamic_list->item_size); + } else{ + // re-estructura a memoria reservada (numero de elementos * tamanho do elemento) + dynamic_list->items = realloc(dynamic_list->items, dynamic_list->length * dynamic_list->item_size); + } + dynamic_list->items[dynamic_list->length-1] = item; +} + + + +void dynamic_list_shift_left(dynamic_list_T* dynamic_list, int index){ + for(int i = index; i < dynamic_list->length-1; i++){ + dynamic_list->items[i] = dynamic_list->items[i+1]; + } +} + + + +int dynamic_list_delete(dynamic_list_T* dynamic_list, int index){ + if(dynamic_list->length > index){ + dynamic_list_shift_left(dynamic_list, index); + dynamic_list->length -= 1; + dynamic_list->items = realloc(dynamic_list->items, dynamic_list->length * dynamic_list->item_size); + return 1; + } + return 0; +} + + + +// Comprobar se o obxeto esta na lista e devolver 1 se esta e foi borrado ou 0 se non esta +int dynamic_list_remove(dynamic_list_T* dynamic_list, void* item, void (*free_method)(void* x)){ + for(int i = 0; i < dynamic_list->length; i++){ + // Se atopas o item na lista borralo + if( dynamic_list->items[i] == item ){ + if (free_method) free_method(item); // Libera memoria do item que se pasa x parametro + dynamic_list_shift_left(dynamic_list, i); + dynamic_list->length -= 1; + dynamic_list->items = realloc(dynamic_list->items, dynamic_list->length * dynamic_list->item_size); + return 1; + } + } + return 0; +} + +void* dynamic_list_get(dynamic_list_T* dynamic_list, int idx){ + return dynamic_list->items[idx]; +} + + +void dynamic_list_free(dynamic_list_T* dynamic_list, void(*free_method)(void* x)){ + for(int i = 0; i < dynamic_list->length; i++){ + free_method(dynamic_list->items[i]); + } + + free(dynamic_list->items); + free(dynamic_list); + +} + diff --git a/lista_dinamica/lista_dinamica.h b/lista_dinamica/lista_dinamica.h @@ -0,0 +1,17 @@ +#ifndef DYNAMIC_LIST_H +#define DYNAMIC_LIST_H + +typedef struct DYNAMIC_LIST_STRUCT{ + size_t item_size; + size_t length; + void** items; +} dynamic_list_T; + +dynamic_list_T* init_dynamic_list(size_t item_size); +void dynamic_list_append(dynamic_list_T* dynamic_list, void* item); +int dynamic_list_find(dynamic_list_T* dynamic_list, void* item, int (*equals_method)(void* x, void* y)); +int dynamic_list_delete(dynamic_list_T* dynamic_list, int index); +void dynamic_list_free(dynamic_list_T* dynamic_list, void(*free_method)(void* x)); + +#endif + diff --git a/lista_dinamica/lista_dinamica.o b/lista_dinamica/lista_dinamica.o Binary files differ. diff --git a/lista_dinamica/main.c b/lista_dinamica/main.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "lista_dinamica.h" + + +struct TEST_STRUCT{ + int id; + char* name; +}; + + + +// Funcion para inicializar o struct test (constructor) +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; +} + +int main(){ + dynamic_list_T* list = init_dynamic_list(sizeof(struct TEST_STRUCT*)); + + //struct TEST_STRUCT* un = init_test_struct("test1"); + + dynamic_list_append(list, init_test_struct("test1")); + dynamic_list_append(list, init_test_struct("test2")); + dynamic_list_append(list, init_test_struct("test3")); + + //dynamic_list_delete(list, 0); + //dynamic_list_remove(list, un, test_free); + + int index = dynamic_list_find(list, init_test_struct("test3"), test_equals); + printf("Posicion a borrar: %d\n", index); + + + for (int i = 0; i < list->length; i++){ + printf("Elemento: %s\n", ((struct TEST_STRUCT*)(list->items[i]))->name); + } + + dynamic_list_free(list, test_free); + + return 0; +} diff --git a/lista_dinamica/main.o b/lista_dinamica/main.o Binary files differ. diff --git a/lista_dinamica/test b/lista_dinamica/test Binary files differ.