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 / laboratorio / lab-19-4-99 / ese8b.c < prev    next >
C/C++ Source or Header  |  1999-05-13  |  2KB  |  99 lines

  1. /* tipo di dato stack di interi: 
  2. in questa versione, lo stack e' una struttura, composta da un array di interi 
  3. e dall'indice della prima posizione libera.
  4. Lo stack viene passato alle funzioni come parametro. Le operazioni sono
  5. implementate come funzioni.
  6.  
  7. In genere non e' buona prassi restituire una struttura come risultato di una
  8. funzione (potrebbe essere molto grande). E' spesso piu' conveniente restituire
  9. un puntatore alla struttura. Provare.
  10. */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #define MAX 10
  17.  
  18.  
  19.  
  20. struct  tstack{
  21.     char stack[MAX];
  22.     int sp;
  23.     } ;        
  24.  
  25.  
  26. /* operazioni sullo stack */ 
  27.  
  28. struct tstack emptystack(void);
  29. int isempty(struct tstack);         /* determina se lo stack e' vuoto */
  30. struct tstack push(int,struct tstack);   /* aggiunge in testa        */
  31. struct tstack pop(struct tstack);        /* toglie elemento in testa */
  32. int   top(struct tstack);        /* restituisce elemento in testa */
  33. void  print(struct tstack);
  34.  
  35. main()
  36.   struct tstack st;
  37.     
  38.     /* costruisco lo stack che contiene i seguenti elementi:
  39.     1 5 6 3 8 (questa e' la testa) */
  40.   st = emptystack();    
  41.   st = push(8,push(3,push(6,push(5,push(1,st)))));
  42.     /* stampo lo stack */
  43.   print(st);
  44.     /*rimuovo la testa e stampo il nuovo stack*/
  45.   st=pop(st);
  46.   print(st);
  47.    /* rimuovo altri due elementi e stampo elemento in testa*/
  48.   st=pop(st);
  49.   st=pop(st);
  50.   printf("\nElemento in testa: %d",top(st));
  51.   return 0;
  52. }
  53.  
  54.  
  55. struct tstack emptystack(void)
  56. {    
  57.     struct tstack st;
  58.     st.sp  = 0;
  59.     return st;
  60. }    
  61.  
  62. int isempty(struct tstack mystack)
  63. {
  64.     return mystack.sp == 0 ;
  65. }    
  66.  
  67. struct tstack push(int el,struct tstack mystack) 
  68.  { 
  69.     if (mystack.sp < MAX)
  70.              mystack.stack[mystack.sp++] = el;         
  71.     else
  72.         printf("\n\nerrore:stack pieno \n\n");
  73.     return mystack;        
  74.  } 
  75.  
  76. struct tstack pop(struct tstack mystack) 
  77.  { 
  78.     if (mystack.sp > 0)
  79.         --mystack.sp;
  80.      return mystack;
  81.  } 
  82.  
  83. int top(struct tstack mystack) 
  84.  { 
  85.     return mystack.stack[mystack.sp -1];
  86.  } 
  87.  
  88.  
  89. void print(struct tstack mystack) 
  90.  { 
  91.     int i;
  92.     printf("\nQuesto e' lo stack: ");
  93.     for(i = 0; i < mystack.sp ; i++)
  94.         printf("%d ",mystack.stack[i]);
  95.     printf("\n");
  96.  } 
  97.  
  98.