Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. // creo una struttura autoreferenziante
  5. //
  6. struct node{
  7.     int data;
  8.     struct node *next;
  9. };
  10.  
  11. typedef struct node Node;
  12. typedef Node *List;
  13. // List e' &Node
  14. // perche' *List e' Node
  15.  
  16. void printList(List head){
  17.     printf("Lista collegata: \n");
  18.     while((head -> next) != NULL){
  19.         printf("%d\n",(head -> data));
  20.         head = (head -> next);
  21.     }
  22.     printf("-----------------\n");
  23. }
  24. void headInsert(List *phead, int val){
  25.     List new;
  26.     // abbiamo creato un puntatore al nodo nuovo (Node *new)
  27.     new = malloc(sizeof(Node));
  28.     // malloc crea un puntatore (di tipo void*), conversione implicita.
  29.     // abbiamo allocato sizeof(Node) byte, e il suo puntatore e' new!
  30.     // ORA DOBBIAMO CONTROLLARE IL SUCCESSO DI MALLOC
  31.     if (new != NULL){
  32.         printf("Memory allocation successful!\n");
  33.         (new -> next) = *phead;
  34.         *phead        = new;
  35.         (new -> data) = val;
  36.     }
  37.     else
  38.         printf("Unable to allocate dynamically memory!\n");
  39.     }
  40. List search(List head,int val){
  41.     int found = 0;
  42.     while((head -> next) != NULL){
  43.         if ((head -> data) == val){
  44.             printf("%d e' il valore cercato.\n",(head -> data));
  45.             return head;
  46.         }
  47.         else{
  48.             printf("%d non e' il valore cercato.\n",(head -> data));
  49.             head = head -> next;
  50.         }
  51.     }
  52. }
  53. void destroy(List head){
  54.     List temp = head;
  55.     List deleting = NULL;
  56.     while((temp -> next) != NULL){
  57.         deleting = temp;
  58.         temp = temp -> next;
  59.         free(deleting);
  60.     }
  61.     head -> next = NULL;
  62. }
  63. void _nullifyNext(List *phead){
  64.     (*phead) -> next = NULL;
  65. }
  66.  
  67. void searchCascadeDestroy(List *phead, int val){
  68.     List cascadeVal = search(*phead,val);
  69.     // ho un puntatore all'elemento dal quale iniziare a cancellare tutto
  70.     destroy(cascadeVal);
  71.     _nullifyNext(&cascadeVal);
  72. }
  73.  
  74. void clusterInsert(int n, int ins[n], List *phead){
  75.     for(int i = 0;i<n;i++)
  76.         headInsert(phead,ins[i]);
  77. }
  78.  
  79. int main(){
  80.     // abbiamo bisogno di un puntatore al primo nodo!
  81.     // List e' &Node
  82.     List head = malloc(sizeof(Node));
  83.     if (head != NULL){
  84.         head -> data = 0;
  85.         head -> next = NULL;
  86.     }
  87.     else
  88.         printf("Head not allocated!\n");
  89.  
  90.     headInsert(&head,1);
  91.     headInsert(&head,2);
  92.     headInsert(&head,3);
  93.     printList(head);
  94.     searchCascadeDestroy(&head,2);
  95.     printList(head);
  96.     int clust[3] = {4,5,6};
  97.     clusterInsert(3,clust,&head);
  98.     printList(head);
  99.     destroy(head);
  100.     printList(head);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement