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-9.00 / parte1.c < prev    next >
C/C++ Source or Header  |  2000-09-20  |  1KB  |  84 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX 30
  5.  
  6. /* TIPI */
  7.  
  8. typedef struct nodo *tree;
  9. struct nodo
  10. {
  11.     tree dx;
  12.     tree sx;
  13.     int info;
  14. };
  15.  
  16.  
  17. /* PROTOTIPI */
  18.  
  19. tree empty(void);
  20. void insert( int, tree *);
  21. void print(tree);
  22.  
  23. /* FUNZIONI */
  24.  
  25. tree empty()
  26. {
  27.     return NULL;
  28. }
  29.  
  30. void insert(int i,tree *t)
  31. {
  32.     if(i>=0)
  33.     {
  34.     
  35.     if((*t)==empty())
  36.     {
  37.         (*t)=(tree)malloc(sizeof(struct nodo));
  38.         (*t)->info=i;
  39.         (*t)->sx=empty();
  40.         (*t)->dx=empty();
  41.     }
  42.     else 
  43.      if(i<(*t)->info)
  44.         insert(i,&((*t)->sx));
  45.      else if(i>(*t)->info)
  46.         insert(i,&((*t)->dx));
  47. }
  48. }
  49.  
  50. void print(tree t)
  51. {
  52.     if(t!=empty())
  53.     {
  54.         print(t->sx);
  55.         printf("%d  ",t->info);
  56.         print(t->dx);
  57.     }
  58. }
  59.  
  60.  
  61. /* MAIN */
  62.  
  63. void main()
  64. {
  65.     char nome[MAX];
  66.     tree t;
  67.     int i;
  68.     FILE *fd=NULL;
  69.     t=empty();
  70.  
  71.     printf("Dammi il nome del file:\n");
  72.     scanf("%s",nome);
  73.  
  74.     if(fd=fopen(nome,"r"))
  75.     {
  76.         while(fscanf(fd,"%d",&i) && i!=-1)
  77.             insert(i,&t);
  78.         print(t);
  79.         fclose(fd);
  80.     }
  81.     else
  82.         printf("Errore di apertura del file");
  83. }
  84.