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-7.99 / turno1 / parte3.c < prev    next >
C/C++ Source or Header  |  1999-07-14  |  2KB  |  172 lines

  1. /* Esame 8 luglio '99 - turno 1 - parte 3 */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define MAX 30
  7.  
  8. typedef struct cell *list ;
  9.  
  10. struct cell
  11. {
  12.   float el;
  13.   list next;
  14. };
  15.  
  16.  
  17.  
  18.  
  19. static int h(float,int);
  20. void insert(float,list*,int);
  21. void print(list*,int);
  22. list* empty(int);
  23. int member(float, list *,int);
  24.  
  25.  
  26. static int h(float i, int n)
  27. {
  28.   int m;
  29.   m = i;
  30.   return m % n;
  31. }
  32.  
  33.  
  34.  
  35. list * empty(int n)
  36. {
  37.    return (list *) calloc(n,sizeof(list));
  38. }
  39.  
  40.  
  41.  
  42. void insert(float i, list t[],int n)
  43. {
  44.   int b=h(i,n);
  45.   list l=t[b],aux, prec=NULL;
  46.  
  47.  
  48.  
  49.   // alloco la nuova cella la nuova cella
  50.  
  51.   aux=(list) malloc(sizeof(struct cell));
  52.   aux->el=i;
  53.   
  54.   // scandisco la lista per trovare la posizione in cui inserire la nuova cella
  55.         
  56.   while (l!=NULL && l->el<i)
  57.     { 
  58.        prec=l;
  59.        l=l->next;
  60.     }  
  61.     
  62.  
  63.   if (prec == NULL && (l == NULL ||  l ->el !=i)) // inserimento in testa
  64.   { 
  65.         aux->next=t[b];
  66.         t[b]=aux;
  67.         return;
  68.      }
  69.      
  70.   
  71.   
  72.   if (l == NULL) // inserimento in fondo
  73.         { 
  74.           
  75.           prec ->next = aux;
  76.           aux ->next = NULL;
  77.           return;
  78.         }     
  79.      
  80.  /* se le prime due condizioni non sono verificate, l'inserimento
  81.     deve essere effettuato nel mezzo della lista */
  82.   
  83.  
  84.   if (l->el !=i) /* l'elemento non e` gia` presente */ 
  85.     {
  86.         prec ->next = aux;
  87.         aux ->next = l;
  88.       
  89.     }  
  90. }
  91.  
  92.  
  93.  
  94. void print(list t[],int n)
  95. {
  96.   int i;
  97.   list l;
  98.   for (i=0;i<n;i++)
  99.     {
  100.       printf("\n%d: ",i);
  101.       for(l=t[i];l!=NULL;l=l->next)
  102.             printf("%f ",l->el);
  103.       printf("\n");
  104.     }
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. int member(float i,list t[],int n)
  113. {
  114.     int b = h(i,n);
  115.     list l = t[b];
  116.     while (l!=NULL)
  117.     {
  118.         if (l->el == i)
  119.             return 1;
  120.         l = l -> next;
  121.     }
  122.     return 0
  123.         ;
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. void main(void)
  131. {
  132.   char fname[MAX];
  133.   list *t;
  134.   FILE *fd=NULL;
  135.   float i,j;
  136.   int n;
  137.  
  138.   printf("\nFornisci la dimensione della tabella: ");
  139.   scanf("%d",&n);
  140.  
  141.     
  142.   // inizializzo tabella
  143.  
  144.   t = empty(n);
  145.  
  146.   printf("\n\nFornisci il nome del file: ");
  147.   scanf("%s",fname);
  148.   if (fd=fopen(fname,"r"))
  149.     {
  150.       while (fscanf(fd,"%f",&i)==1 && i!=0) 
  151.         insert(i,t,n);
  152.       print(t,n);
  153.       fclose(fd);
  154.  
  155.       // determina l'elemento da cercare
  156.  
  157.       printf("\n\nFornisci l'elemento da cercare: ");
  158.       scanf("%f",&j);
  159.       
  160.       if (member(j,t,n))
  161.           printf("\n\nElemento presente\n\n");
  162.       else
  163.           printf("\n\nElemento non presente\n\n");
  164.     }
  165.   else
  166.     printf("Error opening file %s\n",fname);
  167. }
  168.  
  169.  
  170.  
  171.  
  172.