Advertisement
davide1409

list.c

Nov 23rd, 2021
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int get_val(node_t* nodo){
  6.     if(nodo == NULL)
  7.         exit(-20);
  8.     return nodo->val;
  9. }
  10. int init_list(list_t *L){
  11.     if(L == NULL)
  12.         return -1;
  13.  
  14.     L->head = L->tail = NULL;
  15.     return 0;
  16. }
  17.  
  18. int empty(list_t* L){
  19.     if(L == NULL)
  20.         return -1;
  21.  
  22.     return (L->head == NULL);
  23. }
  24.  
  25. node_t* head(list_t* L){
  26.     if(L == NULL)
  27.         return NULL;
  28.  
  29.     return L->head;
  30. }
  31.  
  32. node_t* tail(list_t* L){
  33.     if(L == NULL)
  34.         return NULL;
  35.  
  36.     return L->tail;
  37. }
  38.  
  39. node_t* next(node_t* nodo){
  40.     if(nodo == NULL)
  41.         return NULL;
  42.  
  43.     return nodo->next;
  44. }
  45.  
  46. node_t* prev(list_t* L, node_t* nodo){
  47.     if(L == NULL){
  48.         // termino prima che tu possa generare SEGFAULT
  49.         exit(-1); //return NULL;  // settare errno in maniera opportuna, avrebbe senso terminare -> codice compromesso
  50.     }
  51.  
  52.     if(nodo == NULL)
  53.         return L->tail;
  54.  
  55.     node_t *p = L->head;
  56.     node_t *prev = NULL;
  57.  
  58.     while( (p != nodo) && (p != NULL) ){
  59.         prev = p;
  60.         p = p->next;
  61.     }
  62.    
  63.     return p==NULL? p : prev; // l'unico caso in cui p == NULL è quando non c'è
  64.                               // il nodo(nodo == NULL gestito a parte)
  65.                               // anche quando la lista è vuota
  66. }
  67.  
  68.  
  69. node_t* insert(list_t* L, node_t* nodo, int val){
  70.     if(L == NULL)
  71.         exit(-1);
  72.  
  73.     node_t *new_nodo = malloc(sizeof(*new_nodo));
  74.     if(new_nodo == NULL)
  75.         return NULL;
  76.    
  77.     new_nodo->val = val;
  78.     new_nodo->next = NULL;
  79.     if(head(L) == NULL){
  80.         L->head = L->tail = new_nodo;
  81.     }
  82.  
  83.     else if(nodo == NULL){ // sta inserendo in testa
  84.         new_nodo->next = L->head;
  85.         L->head = new_nodo;
  86.     }
  87.     else{
  88.         new_nodo->next = nodo->next;
  89.         nodo->next = new_nodo;
  90.     }
  91.    
  92.     if(tail(L) == nodo){
  93.         L->tail = new_nodo;
  94.     }
  95.  
  96.  
  97.     return new_nodo;
  98. }
  99.  
  100.  
  101. node_t *delete(list_t *L, node_t* nodo){
  102.     if(L == NULL)
  103.         exit(-1);
  104.  
  105.     if(empty(L))
  106.         return NULL;
  107.    
  108.     if(nodo == NULL)
  109.         return NULL; // dovresti settare errno
  110.  
  111.     // la lista è valida e pure il nodo
  112.    
  113.     node_t* prec = prev(L, nodo); // mi ricavo il precedente
  114.     if(nodo == head(L)){
  115.         L->head = L->head->next;
  116.     }
  117.  
  118.     else{  
  119.         if(prec == NULL) // avendo escluso gli altri, l'unico caso è che il nodo non c'è
  120.             return NULL;
  121.  
  122.         prec->next = nodo->next;
  123.     }
  124.    
  125.     if(nodo == tail(L))
  126.         L->tail = prec;
  127.  
  128.  
  129.     free(nodo);
  130.  
  131.     return prec; // NULL solo se nodo == head
  132. }
  133.  
  134. void print_list(list_t* L){
  135.     if(L == NULL){
  136.         printf("Lista nulla!\n");
  137.         return;
  138.     }
  139.  
  140.     if(empty(L)){
  141.         printf("Lista vuota!\n");
  142.         return;
  143.     }
  144.    
  145.     node_t *p = L->head;
  146.     while(p != NULL){
  147.         printf("|%d|->", p->val);
  148.         p = p->next;
  149.     }
  150.     puts("NULL");
  151.    
  152.     return;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement