Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. struct node{
  2.     int data;
  3.     struct node *next;
  4. };
  5.  
  6. typedef struct node Node;
  7. typedef Node *List;
  8.  
  9. void printList(List head){
  10.     printf("Lista collegata: \n");
  11.     while((head -> next) != NULL){
  12.         printf("%d\n",(head -> data));
  13.         head = (head -> next);
  14.     }
  15.     printf("-----------------\n");
  16. }
  17. void headInsert(List *phead, int val){
  18.     List new;
  19.     // abbiamo creato un puntatore al nodo nuovo (Node *new)
  20.     new = malloc(sizeof(Node));
  21.     // malloc crea un puntatore (di tipo void*), conversione implicita.
  22.     // abbiamo allocato sizeof(Node) byte, e il suo puntatore e' new!
  23.     // ORA DOBBIAMO CONTROLLARE IL SUCCESSO DI MALLOC
  24.     if (new != NULL){
  25.         printf("Memory allocation successful!\n");
  26.         (new -> next) = *phead;
  27.         *phead        = new;
  28.         (new -> data) = val;
  29.     }
  30.     else
  31.         printf("Unable to allocate dynamically memory!\n");
  32. }
  33.  
  34. void clusterInsert(int n, int ins[n], List head){
  35.     for(int i = 0;i<n;i++)
  36.         headInsert(&head,ins[i]);
  37. }
  38.  
  39. int main(){
  40.     // abbiamo bisogno di un puntatore al primo nodo!
  41.     // List e' &Node
  42.     List head = malloc(sizeof(Node));
  43.     if (head != NULL){
  44.         head -> data = 0;
  45.         head -> next = NULL;
  46.     }
  47.     else
  48.         printf("Head not allocated!\n");
  49.  
  50.     headInsert(&head,1);
  51.     headInsert(&head,2);
  52.     headInsert(&head,3);
  53.     printList(head);
  54.     searchCascadeDestroy(&head,2);
  55.     printList(head);
  56.     int clust[3] = {4,5,6};
  57.     clusterInsert(3,clust,head);
  58.     printList(head);
  59.     destroy(head);
  60.     printList(head);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement