ccousas

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

lista_nodos.c (2805B)


      1 #include <stdlib.h>
      2 #include "lista_nodos.h"
      3 
      4 
      5 NODE_LIST_T* init_node_list() {
      6     NODE_LIST_T* list = malloc(sizeof(NODE_LIST_T));
      7     list->head = NULL;
      8     list->tail = NULL;
      9     list->length = 0;
     10     return list;
     11 }
     12 
     13 
     14 int node_list_append(void* item, NODE_LIST_T* list) {
     15     NODE_T* node = malloc(sizeof(NODE_T));
     16     node->item = item;
     17     node->sig = NULL;
     18     if (list->head == NULL) list->head = node;
     19     else list->tail->sig = node;
     20 
     21     list->tail = node;
     22     list->length++;
     23     return 1;
     24 }
     25 
     26 
     27 // Devolve NULL se o indice e maior que o tamanho da lista
     28 // ou se a lista e baleira, de outra forma devolve void* item
     29 void* node_list_get(int index, NODE_LIST_T* list) {
     30     if (list->head == NULL) return NULL;
     31     else{
     32         NODE_T* tmp = list->head;
     33         for(int i = 0; i < index; i++){
     34             if(tmp->sig == NULL) return NULL;
     35             else tmp = tmp->sig;
     36         }
     37         return tmp->item;
     38     }
     39 }
     40 
     41 
     42 void node_list_print(NODE_LIST_T* list, void (*print_method)(void* x, int y)) {
     43     NODE_T* tmp = list->head;
     44     for(int i = 0; i < list->length; i++){
     45         print_method(tmp->item, i);
     46         tmp = tmp->sig;
     47     }
     48 }
     49 
     50 
     51 // Devolve o indice do obxeto ou -1 senon esta
     52 int node_list_find(NODE_LIST_T* list, void* item, int (*equals_method)(void* x, void* y)) {
     53     NODE_T* tmp = list->head;
     54     for(int i = 0; i < list->length; i++){
     55         if (equals_method(tmp->item, item)){
     56             return i;
     57         }
     58         tmp = tmp->sig;
     59     }
     60     return -1;
     61 }
     62 
     63 
     64 // Elimina un elemento dada a posicion na lista (devolve 0 se non se puido eliminar, 1 se si se puido)
     65 int node_list_delete(int index, NODE_LIST_T* list, void (*free_method)(void* x)) {
     66     if (list->head == NULL) return 0;
     67     else if (list->head == list->tail) {
     68         // Deixar a lista baleira
     69         if (free_method) free_method(list->head->item);
     70         free(list->head);
     71         list->head = NULL;
     72         list->tail = NULL;
     73     }
     74     else if (index == 0) {
     75         NODE_T* tmp = list->head;
     76         list->head = list->head->sig;
     77         if (free_method) free_method(tmp->item);
     78         free(tmp);
     79     } 
     80     else{ 
     81         NODE_T** tmp = &list->head;
     82         NODE_T* aux;
     83         for(int i = 0; i < index; i++){
     84             if((*tmp)->sig == NULL) return 0;
     85             else {
     86                 tmp = &((*tmp)->sig);
     87             }
     88         }
     89         aux = *tmp;
     90         *tmp = (*tmp)->sig;
     91         
     92         free_method(aux->item);
     93         free(aux);
     94     }
     95     list->length--;
     96     return 1;
     97 }
     98 
     99 
    100 void node_list_free(NODE_LIST_T* list, void(*free_method)(void* x)) {
    101     if (list->head == NULL) return;
    102     NODE_T* tmp = list->head;
    103     NODE_T* aux;
    104     for(int i = 0; i < list->length; i++){
    105         aux = tmp;
    106         tmp = tmp->sig;
    107         free_method(aux->item);
    108         free(aux);
    109     }
    110     free(list);
    111 }