Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Matrice sparsa con delle liste linkate.
Forum - C/C++ - Matrice sparsa con delle liste linkate.

Avatar
comtel (Member)
Pro


Messaggi: 145
Iscritto: 08/04/2011

Segnala al moderatore
Postato alle 23:36
Lunedì, 01/10/2018
Salve ragazzi, il mio problema riguarda in particolare la prima parte della traccia dell'esercizio che sto risolvendo. Quello che chiede innanzitutto è di creare una matrice sparsa con delle liste linkate, questo è quindi il codice che ho implementato:

Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. struct node_elem {
  7.         short info;
  8.         short row;
  9.         short column;
  10.         struct node_elem *next_elem;
  11. };
  12.  
  13. struct node_row {
  14.         short row_num;
  15.         struct node_row *next_row;
  16.         struct node_elem *first_elem;
  17. };
  18.  
  19. typedef struct node_elem ELEMENT;
  20. typedef ELEMENT *ELINK;
  21. ELINK epunt;
  22.  
  23. typedef struct node_row ROW;
  24. typedef ROW *LINK;
  25. LINK head, punt;
  26.  
  27. void * make_rows();
  28. void mat_1_maker(short, short);
  29. void head_inseriment(short, ROW *, ROW **);
  30. void elem_inseriment(short, ELEMENT *, ELEMENT **);
  31.  
  32. int main(int argc, char *argv[]) {
  33.     srand(time(NULL));
  34.         head = (ROW *)make_rows();
  35.     mat_1_maker(4, 5);
  36.    
  37.     punt = head;
  38.     while(punt != NULL) {
  39.         printf("Row %hd\t -> ", punt->row_num);
  40.        
  41.         if((epunt = punt->first_elem) == NULL) {
  42.             printf("Empty row..\n");
  43.         } else {
  44.             while(epunt != NULL) {
  45.                 printf("Elem: %hd\t", epunt->info);
  46.                 epunt = epunt->next_elem;
  47.             }
  48.         }
  49.        
  50.         punt = punt->next_row;
  51.         printf("\n");
  52.     }
  53. }
  54.  
  55. void * make_rows() {
  56.         char *head = NULL;
  57.         return head;
  58. }
  59.  
  60. void mat_1_maker(short rows, short columns) {
  61.     short i = 0, j = 0;
  62.     ROW *row_ptr;
  63.     ELEMENT *ele_ptr;
  64.    
  65.     for(i = 0; i < rows; i++) {
  66.         if(rand()%2 == 0) {
  67.             row_ptr = (ROW *)calloc(1, sizeof(ROW));
  68.             row_ptr->row_num = i;
  69.             row_ptr->first_elem = (ELEMENT *)calloc(1, sizeof(ELEMENT));
  70.             head_inseriment(sizeof(ROW), row_ptr, &head);
  71.            
  72.             for(j = 0; j < columns; j++) {
  73.                 if(rand()%2 != 0) {
  74.                     (row_ptr->first_elem)->info = rand() % 100;
  75.                     (row_ptr->first_elem)->row = i;
  76.                     (row_ptr->first_elem)->column = j;
  77.                    
  78.                     elem_inseriment(sizeof(ELEMENT), row_ptr->first_elem, &(row_ptr->first_elem));
  79.                 }
  80.             }
  81.            
  82.             free(row_ptr->first_elem);
  83.             free(row_ptr);
  84.         }
  85.     }
  86. }
  87.  
  88. void head_inseriment(short len_info, ROW *data, ROW **head) {
  89.     ROW *ptr = (ROW *)calloc(1, sizeof(ROW));
  90.     memcpy(ptr, data, len_info);
  91.    
  92.     ptr->next_row = *head;
  93.     *head = ptr;
  94. }
  95.  
  96. void elem_inseriment(short len_info, ELEMENT *data, ELEMENT **first_elem) {
  97.     ELEMENT *ptr = (ELEMENT *)calloc(1, sizeof(ELEMENT));
  98.     memcpy(ptr, data, len_info);
  99.    
  100.     ptr->next_elem = *first_elem;
  101.     *first_elem = ptr;
  102. }



Praticamente il mio problema sta nel fatto che nonostante il ciclo FOR che viene ripetuto tante quante sono le colonne che vogliamo nella matrice cicli, ebbene nonostante questo, vi è sempre uno ed un solo elemento che viene inserito nella lista che dovrebbe contenere gli elementi non nulli della matrice. Non riesco a capire il perchè di questo comportamento sinceramente.. :-?

PM Quote
Avatar
comtel (Member)
Pro


Messaggi: 145
Iscritto: 08/04/2011

Segnala al moderatore
Postato alle 16:24
Martedì, 02/10/2018
Se qualcuno è incappato nel mio stesso errore, vorrei proporre una mia soluzione al problema:

Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. struct node_elem {
  7.         short info;
  8.         short row;
  9.         short column;
  10.         struct node_elem *next_elem;
  11. };
  12.  
  13. struct node_row {
  14.         short row_num;
  15.         struct node_row *next_row;
  16.         struct node_elem *first_elem;
  17. };
  18.  
  19. typedef struct node_elem ELEMENT;
  20. typedef ELEMENT *ELINK;
  21. ELINK ehead, epunt;
  22.  
  23. typedef struct node_row ROW;
  24. typedef ROW *LINK;
  25. LINK head, punt;
  26.  
  27. void * make_rows();
  28. void mat_1_maker(short, short);
  29. void head_inseriment(short, ROW *, ROW **);
  30. void elem_inseriment(short, ELEMENT *, ELEMENT **);
  31.  
  32. int main(int argc, char *argv[]) {
  33.     srand(time(NULL));
  34.         head = (ROW *)make_rows();
  35.     mat_1_maker(4, 5);
  36.    
  37.     punt = head;
  38.     while(punt != NULL) {
  39.         printf("Row %hd\t -> ", punt->row_num);
  40.        
  41.         if((epunt = ehead) == NULL) {
  42.             printf("Empty row..\n");
  43.         } else {
  44.             while(epunt != NULL) {
  45.                 printf("Elem: %hd\t", epunt->info);
  46.                 epunt = epunt->next_elem;
  47.             }
  48.         }
  49.        
  50.         punt = punt->next_row;
  51.         printf("\n");
  52.     }
  53. }
  54.  
  55. void * make_rows() {
  56.         char *head = NULL;
  57.         return head;
  58. }
  59.  
  60. void mat_1_maker(short rows, short columns) {
  61.     short i = 0, j = 0;
  62.     ROW *row_ptr;
  63.     ELEMENT *ele_ptr;
  64.    
  65.     for(i = 0; i < rows; i++) {
  66.         if(rand()%2 == 0) {
  67.             row_ptr = (ROW *)calloc(1, sizeof(ROW));
  68.             row_ptr->row_num = i;
  69.             row_ptr->first_elem = (ELEMENT *)calloc(1, sizeof(ELEMENT));
  70.            
  71.             ehead = NULL;
  72.            
  73.             head_inseriment(sizeof(ROW), row_ptr, &head);
  74.            
  75.             for(j = 0; j < columns; j++) {
  76.                 if(rand()%2 != 0) {
  77.                     (row_ptr->first_elem)->info = rand() % 100;
  78.                     (row_ptr->first_elem)->row = i;
  79.                     (row_ptr->first_elem)->column = j;
  80.                    
  81.                     elem_inseriment(sizeof(ELEMENT), row_ptr->first_elem, &ehead);
  82.                 }
  83.             }
  84.            
  85.             free(row_ptr->first_elem);
  86.             free(row_ptr);
  87.         }
  88.     }
  89. }
  90.  
  91. void head_inseriment(short len_info, ROW *data, ROW **head) {
  92.     ROW *ptr = (ROW *)calloc(1, sizeof(ROW));
  93.     memcpy(ptr, data, len_info);
  94.    
  95.     ptr->next_row = *head;
  96.     *head = ptr;
  97. }
  98.  
  99. void elem_inseriment(short len_info, ELEMENT *data, ELEMENT **ehead) {
  100.     ELEMENT *ptr = (ELEMENT *)calloc(1, sizeof(ELEMENT));
  101.     memcpy(ptr, data, len_info);
  102.    
  103.     ptr->next_elem = *ehead;
  104.     *ehead = ptr;
  105. }


PM Quote