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-6.99 / turno1 / parte3.c < prev    next >
C/C++ Source or Header  |  1999-06-16  |  2KB  |  150 lines

  1. /* Una possibile soluzione Esame di Laboratorio di Algoritmi 15 giugno 1999
  2.  * Turno 1 */
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define MAX 20
  9.  
  10. /* definizione dei tipi */
  11.  
  12. typedef  struct stackel * stack;    /* uno stack  e' un puntatore */
  13. typedef  struct stackel{ int el; stack next; } stck;
  14.  
  15. /* operazioni sullo stack */ 
  16.  
  17. stack   empty(void);       /* restituisce uno stack vuoto, cioe' restituisce NULL */
  18. stack push(int,stack);     /* aggiunge in testa        */
  19. void  print(stack);        /* stampa lo stack */
  20. stack makestackel(void);   /* alloca una nuova cella */
  21. stack pop(stack);            /* toglie elemento in testa */
  22. int   top(stack);         /* restituisce elemento in testa */
  23.  
  24.  
  25. /* main */  
  26.  
  27. main()
  28. {
  29.   
  30.     FILE * fp;
  31.     char nome[MAX];
  32.     int n;
  33.     stack s;
  34.  
  35.   
  36.     /* creo stack vuoto */
  37.  
  38.     s = empty();
  39.  
  40.     /* leggo il nome del file */
  41.  
  42.     printf("Fornisci il nome del file: ");
  43.     scanf("%s", nome);
  44.  
  45.     /* apro il file in lettura */
  46.  
  47.     fp = fopen(nome,"r");
  48.  
  49.  
  50.     /* carico lo stack con i dati letti da file 
  51.      * (assumendo il file del formato corretto */
  52.  
  53.     while (fscanf(fp,"%d",&n) && n != -1)
  54.         s = push(n,s);
  55.  
  56.     /* stampo lo stack */
  57.  
  58.     print(s);
  59.  
  60.  
  61.     /* determino l'elemento in testa */
  62.  
  63.     n = top(s);
  64.  
  65.     /* stampo l'elemento in testa, se lo stack non e' vuoto */
  66.  
  67.     if (n != -1)
  68.         printf("\nL'elemento in testa allo stack e' %d\n",n);
  69.     else
  70.         printf("\nLo stack non contiene elementi\n");
  71.  
  72.  
  73.     /* cancello i primi due elementi dello stack */
  74.  
  75.     s = pop(pop(s));
  76.     printf("\nHo cancellato i primi due elementi\n");
  77.  
  78.  
  79.     /* determino l'elemento in testa */
  80.  
  81.     n = top(s);
  82.  
  83.     /* stampo l'elemento in testa, se lo stack non e' vuoto */
  84.  
  85.  
  86.  
  87.     if (n != -1)
  88.         printf("\nL'elemento in testa allo stack e' %d\n",n);
  89.     else
  90.         printf("\nLo stack non contiene elementi\n");
  91.  
  92. }
  93.  
  94.  
  95. /* funzioni */
  96.  
  97.  
  98. stack empty()
  99.  { 
  100.   return NULL;  
  101.  }
  102.  
  103.  
  104.  
  105. stack push(int el, stack s) 
  106.  { 
  107.    stack t;
  108.  
  109.    t=makestackel();      
  110.    t->el=el;
  111.    t->next=s;
  112.    return t; 
  113.  } 
  114.  
  115.  
  116.  
  117. stack pop(stack s) 
  118.  { 
  119.    stack t;
  120.  
  121.    if (s == NULL) return NULL;  
  122.    else 
  123.     {
  124.       t=s;
  125.       s=s->next;
  126.       free(t);
  127.       return s; 
  128.     } 
  129.  } 
  130.  
  131.  
  132. int top(stack s) 
  133.  { 
  134.    if (s == NULL) return -1;  
  135.    else  return s->el;  
  136.  } 
  137.  
  138.  
  139. stack makestackel(){ return (stack)malloc(sizeof(stck)); }
  140.  
  141.  
  142. void print(stack s) 
  143. {  
  144.     printf("\nLo stack ha il seguente contenuto: ");
  145.     for(; s !=NULL ; s = s -> next)
  146.        printf(" %d",s->el);
  147.     printf("\n\n");
  148.  } 
  149.  
  150.