Advertisement
davide1409

31-08-2020.c

Jan 19th, 2021
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. /* nella funzione chiamante la lista L viene dichiarata come segue: struct node *L;
  4.  * la funzione da realizzare invece viene invocata in questa maniera: int *ptr = Vetmaggiori(L);
  5.  * ptr rappresenta il generico nome che si può dare ad un puntatore.
  6.  */
  7.  
  8.  
  9. int num_rows(struct node *list){
  10.     struct node *cur = list->next;      // list punta il nodo sentinella
  11.     int row = 0;
  12.  
  13.     while(cur){
  14.         row = row > cur->row? row : cur->row;
  15.         cur = cur->next;
  16.     }
  17.  
  18.     return ++row;
  19. }
  20.  
  21.  
  22. int *maxrows(struct node * const list){
  23.     int row = num_rows(list);           // mi restituisce il numero di righe della lista
  24.     int *v = malloc(sizeof(*v) * row)
  25.     if(!v){
  26.         return NULL;
  27.     }
  28.    
  29.     int check_rows[row];
  30.  
  31.     //inizializzo massimi e il vettore di controllo riga
  32.     for(int i = 0; i!=row; i++){
  33.         v[i] = 0;
  34.         check_rows[i] = 0;
  35.     }
  36.    
  37.     struct node *cur = list->next;      // list punta il nodo sentinella
  38.     struct node *ptr = cur;
  39.     while(cur){
  40.         if(!check_rows[cur->row]){
  41.             while(ptr){
  42.                 if(ptr->row == cur->row){
  43.                     v[ptr->row] = ptr->elem > v[ptr->row]? ptr->elem : v[ptr->row];
  44.                 }
  45.                 ptr = ptr->next;
  46.             }
  47.        
  48.             check_rows[cur->row] = 1;
  49.         }
  50.  
  51.         cur = cur->next;
  52.         ptr = list->next; // perchè list punta alla sentinella e io voglio il primo elementoi
  53.     }
  54.  
  55.     return v;
  56.  
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement