Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. struct node {
  6.  int val;
  7.  int ind;
  8.  struct node *next;
  9. };
  10.  
  11. // stampa lista
  12. void print_list(struct node *list_head){
  13.  
  14.     // lista vuota
  15.     if (list_head == NULL){
  16.         printf("lista vuota!\n");
  17.         return;
  18.     }
  19.  
  20.     struct node *iteratore = list_head;
  21.     int indice = 0;
  22.     int counter = 0;
  23.     int max = iteratore->val;
  24.     int min = iteratore->val;
  25.  
  26.     while (iteratore != NULL){
  27.         printf("(%d, %d)->", iteratore->val, iteratore->ind);
  28.         if (iteratore->val > max){
  29.             max = iteratore->val;
  30.         }
  31.         if (iteratore->val < min){
  32.             min = iteratore->val;
  33.         }
  34.         counter+= iteratore->val;
  35.         iteratore = iteratore->next;
  36.         indice += 1;
  37.     }
  38.     printf("\b\b  \n");
  39.     printf("Max: %d, Min: %d, Media: %d\n\n", max, min, counter/indice);
  40.  
  41.     return;
  42. }
  43.  
  44. struct node *createnode(int x, int i){
  45.  
  46.     struct node *node = (struct node*)malloc(sizeof(struct node));
  47.     if (node == NULL){
  48.         printf("malloc() failed!\n");
  49.         exit(1);
  50.     }
  51.     node->val = x;
  52.     node->ind = i;
  53.  
  54.     return node;
  55. }
  56.  
  57. void insertnode(struct node **list_head, int x, int i){
  58.  
  59.     // primo elemento da inserire in lista
  60.     if (*list_head == NULL){
  61.         struct node *node = createnode(x,i);
  62.         node->next = NULL;
  63.         *list_head = node;
  64.         return;
  65.     }
  66.  
  67.     // scorrere la lista fino al penultimo nodo
  68.     struct node *iteratore = *list_head;
  69.     while (iteratore->next != NULL){
  70.         iteratore = iteratore->next;
  71.     }
  72.     struct node *node = createnode(x,i);
  73.     iteratore->next = node;
  74.     node->next = NULL;
  75.     return;
  76. }
  77.  
  78. struct node *arrayList(int V[], int len){
  79.  
  80.     struct node *list_head = NULL;
  81.  
  82.     int i = 0;
  83.     for (i = 0; i < len; i++){
  84.         if (V[i] != 0){
  85.             insertnode(&list_head, V[i], i);
  86.         }
  87.     }
  88.  
  89.     return list_head;
  90. }
  91.  
  92. void updateList(struct node **list_head, int value, int index){
  93.  
  94.     struct node *iteratore = *list_head;
  95.     while(iteratore->next != NULL){
  96.        
  97.         // indice presente in lista
  98.         if (iteratore->ind == index){
  99.             iteratore->val = value;
  100.             return;
  101.         }
  102.  
  103.         // indice non presente in lista
  104.         if (iteratore->ind < index && index < iteratore->next->ind){
  105.             struct node *node = createnode(value, index);
  106.             node->next = iteratore->next;
  107.             iteratore->next = node;
  108.             return;
  109.         }
  110.         iteratore = iteratore->next;
  111.     }
  112.  
  113.     // indice non presente in lista fino al penultimo nodo
  114.     if (iteratore->ind == index){
  115.         iteratore->val = value;
  116.         return;
  117.     }
  118.     else{
  119.         struct node *node = createnode(value, index);
  120.         iteratore->next = node;
  121.         node->next = NULL;
  122.         return;
  123.     }
  124.    
  125.     return;
  126. }
  127.  
  128.  
  129. int main(void){
  130.  
  131.     int len = 6;
  132.     int V[6] = { 1, 0, -1, 6, 0, 4 };
  133.     struct node *list_head;
  134.  
  135.  /* Invocare la funzione "arrayList" con l'array V come argomento */
  136.  
  137.     list_head= arrayList(V, len);
  138.     print_list(list_head);
  139.  
  140.  /* Invocare la funzione "updateList" con la lista creata da arrayList,
  141.  value=7 e index=2 come parametri */
  142.  
  143.     updateList(&list_head, 7, 2);
  144.     print_list(list_head);
  145.  
  146.  /* Invocare la funzione "updateList" con la lista, value=8 e index=1 come
  147.  parametri */
  148.  
  149.     updateList(&list_head, 8, 1);
  150.     print_list(list_head);
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement