home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 308_01 / stack.c < prev    next >
Text File  |  1990-06-17  |  2KB  |  100 lines

  1.  
  2.  
  3. /*
  4.     HEADER:         CUG308;
  5.     TITLE:          Generic push-down stack using
  6.             LIST module;
  7.     DESCRIPTION:    "Uses a List object to implement a
  8.             generic push-down stack.";
  9.     KEYWORDS:       stack generic;
  10.     DATE:           5/6/90;
  11.     VERSION:    2.01;
  12.     FILENAME:       STACK.C;
  13.     REQUIREMENTS:    STACK.H, DEBUG.OBJ, DEBUG.H, LIST.OBJ, LIST.H;
  14.     USAGE:          "link stack.obj, debug.obj and list.obj with
  15.             your main program";
  16.     SEE-ALSO:       STACK.H, LIST.DOC, LIST.C, LIST.H, DEBUG.H;
  17.  
  18.     AUTHOR:         Michael Kelly
  19.             254 Gold St. Boston Ma. 02127;
  20.     COPYRIGHT:    May be used freely if authorship is acknowledged;
  21. */
  22.  
  23.  
  24. /*
  25.  *  STACK
  26.  */
  27.  
  28.  
  29. #define DEBUG 1
  30.  
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <stddef.h>
  34. #include "debug.h"
  35. #include "list.h"
  36. #include "stack.h"
  37.  
  38. static int dummy();                /* placeholder for initlist() function  */
  39. static void init_stack(void);
  40. static List stack;
  41.  
  42.  
  43. static void init_stack(void)
  44. {
  45.     if(! initlist(&stack, dummy))
  46.     err_exit("\n\n\tError initializing Stack!\n");
  47. }
  48.  
  49.  
  50. int push(void *data, size_t datasize)
  51. {
  52.     /*
  53.      *  if pointer to compare function is NULL,
  54.      *  stack has not been initialized;
  55.      *  if stack.id is -1, stack has been "destroyed"
  56.      */
  57.     if(! stack.compare  ||  stack.id == -1)
  58.     init_stack();
  59.  
  60.     return stack.add(stack.id, data, datasize, FIRST);
  61. }
  62.  
  63.  
  64. int pop(void *databuf)
  65. {
  66.     if(! stack.compare  ||  stack.id == -1)
  67.     return 0;
  68.  
  69.      return stack.remitem(stack.id, databuf);
  70. }
  71.  
  72. int copytos(void *databuf)
  73. {
  74.     if(! stack.compare  ||  stack.id == -1)
  75.     return 0;
  76.  
  77.  
  78.     return stack.getitem(stack.id, databuf);
  79. }
  80.  
  81. size_t tossize(void)
  82. {
  83.     if(! stack.compare  ||  stack.id == -1)
  84.     return 0;
  85.  
  86.     return stack.getsize(stack.id);
  87. }
  88.  
  89.  
  90. /*
  91.  *  placeholder for initlist() cmpfunc parameter
  92.  *  no comparison function is needed since we will not sort the stack
  93.  */
  94. static int dummy()
  95. {
  96.     return 0;    /* all things being equal */
  97. }
  98.  
  99.  
  100.