home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / object / STstack.c < prev    next >
C/C++ Source or Header  |  1992-04-09  |  953b  |  76 lines

  1. /*
  2.  * Try to implement a stack-object
  3.  */
  4.  
  5. #include "STstack.h"
  6.  
  7. int 
  8. STnew(st, max)
  9.   Stack *st;
  10.   int max;
  11. {
  12.      char *space;
  13.  
  14.      space = malloc(sizeof(char*) * max);
  15.      /** allocated some pointers **/
  16.      STsetItemptr(st, space);
  17.      STsetMax(st, max);
  18.      STsetPtr(st, 0);
  19. }
  20.  
  21.  
  22. void
  23. STdestroy(st)
  24.   Stack *st;
  25. {
  26.      char *moo;
  27.  
  28.      moo = STgetItem(st, 0);
  29.  
  30.      if (moo != NULL) {
  31.       free(moo);
  32.      }
  33. }
  34.  
  35.  
  36. int
  37. STpop(st, datum)
  38.   Stack *st;
  39.   char *datum;
  40. {
  41.      int level;
  42.  
  43.      level = STgetPtr(st);
  44.  
  45.      if (level ==0)
  46.       return(-1);
  47.      else
  48.       level--;
  49.  
  50.      STsetPtr(st, level);
  51.  
  52.      datum = STgetItem(st, level);
  53.      
  54.      return(0);
  55. }
  56.  
  57. int 
  58. STpush(st, ptr)
  59.   Stack *st;
  60.   char *ptr;
  61. {
  62.      int level;
  63.  
  64.      level = STgetPtr(st);
  65.  
  66.      if (STgetMax(st) <= level)
  67.       return(-1);
  68.  
  69.      /* Add the pointer to the list */
  70.      STsetItem(st, STgetPtr(st), ptr);
  71.  
  72.      /* Increase the level */
  73.      level++;
  74.      STsetPtr(st, level);
  75. }
  76.