home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / testi-esami / labo-10.99 / parte4.c < prev    next >
C/C++ Source or Header  |  2001-01-29  |  3KB  |  165 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 100
  4.  
  5.  
  6.  
  7.  
  8. typedef unsigned char bool;
  9. typedef int elem;
  10.  
  11. typedef struct cell * mset;
  12.  
  13. typedef struct cell {
  14.                         elem el;
  15.                         int n;
  16.                         mset next;
  17.                     }cell;
  18.  
  19.  
  20.  
  21. mset makelem();
  22. mset empty();
  23. mset insert(elem,mset);
  24. void print(mset);
  25. void reverse(mset *);
  26.  
  27.  
  28.  
  29. int main()
  30. {
  31.     mset s = empty();
  32.     int m;
  33.     FILE *fp;
  34.     char nome_f[20];
  35.     
  36.     printf("\nInserire il nome del file ");
  37.     scanf("%s",nome_f);
  38.  
  39.     fp=fopen(nome_f,"r");
  40.  
  41.     while (fscanf(fp,"%d",&m) && (m!=-1))
  42.     {
  43.         s=insert(m,s);
  44.     }
  45.     printf("Il multi-insieme e': ");
  46.     print(s);
  47.  
  48.     reverse(&s);
  49.  
  50.     printf("Il multi-insieme rovesciato e': ");
  51.     print(s);
  52.  
  53. }
  54.  
  55.  
  56. /* definizione delle operazioni come funzioni. 
  57.  * Provare a darne una versione ricorsiva */
  58.  
  59.  
  60. mset makelem()
  61. {
  62.     return (mset) malloc(sizeof(cell));
  63. }
  64.  
  65.  
  66.  
  67.  
  68. mset empty()
  69. {
  70.   return NULL;
  71. }
  72.  
  73.  
  74. mset insert(elem el,mset s)
  75. {
  76.     mset prescorri,scorri, aux;
  77.  
  78.     
  79.     /* cerco la posizione (avanzo fino a trovare un elemento >= el o fine
  80.      * lista)*/
  81.     
  82.     for (prescorri = empty(), scorri = s; scorri != empty() &&
  83.       (scorri ->el < el); prescorri =scorri, scorri = scorri->next);
  84.     
  85.     if (scorri != empty()) /* ho trovato elemento >= el */
  86.        {
  87.           if (scorri -> el > el) /* el non e' presente, quindi lo devo
  88.                                   * aggiungere */
  89.               {
  90.                   if (prescorri != empty()) /* l'inserimento deve essere
  91.                                              * effettuato all'interno della
  92.                                              * lista */
  93.                                {aux = makelem();
  94.                             aux ->el = el;
  95.                     aux ->n =1;    
  96.                             prescorri ->next = aux;
  97.                                 aux ->next = scorri;
  98.                                 return s;}   
  99.                   else                      /* inserimento in prima
  100.                                              * posizione */
  101.                                              
  102.                                { aux = makelem();
  103.                                 aux ->el = el; 
  104.                               aux->n  =1;        
  105.                             aux->next =s;
  106.                          return aux;}    
  107.               }          
  108.           else /* elemento presente, aumento il numero di occorrenze */
  109.          { (scorri->n)++;    
  110.                 return s;               
  111.                  }
  112.        }
  113.     else /* la lista e' vuota o devo inserire in ultima posizione */
  114.        {
  115.           if (prescorri!= empty()) /* inserimento in ultima posizione */
  116.                               { aux = makelem();
  117.                             aux ->el = el;
  118.                             aux ->n = 1;
  119.                             prescorri ->next = aux;
  120.                                 aux ->next = scorri;
  121.                                 return s;
  122.                               }
  123.           
  124.           else /* la lista era vuota */
  125.                                {aux = makelem();
  126.                               aux ->el = el; 
  127.                            aux ->n = 1;
  128.                           aux->next =s;
  129.                        return aux;   }
  130.        }   
  131. }
  132.  
  133.  
  134.  
  135. void print(mset s)
  136. {
  137.   int i;
  138.   printf("{ ");
  139.   for (; s!=empty() ; s = s ->next)
  140.     { 
  141.     for(i=0;i<s->n;i++)
  142.          printf("%d ",s->el);
  143.     }
  144.   printf("}\n");
  145. }
  146.  
  147.  
  148.  
  149.  
  150. void reverse(mset *s)
  151. {
  152.     mset corr = *s;
  153.     mset prec=empty(); /*precedente */
  154.     mset succe = corr; /*successivo */
  155.     
  156.     while (corr != NULL)
  157.     {
  158.       succe = succe->next;
  159.       corr->next = prec;
  160.       prec= corr;
  161.       corr= succe;
  162.     }
  163.     *s = prec; /*l'ultimo precedente punta all'ultima locazione della vecchia successione cioe' alla prima della nuova */
  164. }
  165.