home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STK.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  97 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  STK.C - Stack manager
  5. **
  6. **  by Dustin Puryear (dpuryear@intersurf.com), placed into Public Domain.
  7. */
  8.  
  9. #include <stdio.h>       /* General include file */
  10. #include <stdlib.h>      /* malloc() and free()  */
  11. #include "stk.h"         /* Function prototypes  */
  12.  
  13. void stkInit(Stk *pstack)
  14. {
  15.       pstack->ptop = NULL;
  16.       pstack->vcount = 0;
  17. }
  18.  
  19. int stkPush(Stk *pstack, void *pdata)
  20. {
  21.       StkNode *padd;   /* Node to add to top of stack */
  22.  
  23.       /*
  24.       ** Create the node to append to the top of the stack. Also,
  25.       ** check for a memory allocation error.
  26.       */
  27.  
  28.       padd = (StkNode *) malloc(sizeof(StkNode));
  29.       if ( padd == NULL )
  30.       {
  31.             return (0);
  32.       }
  33.  
  34.       /*
  35.       ** Now, add the data pointer to the node.
  36.       */
  37.  
  38.       padd->pdata = pdata;
  39.  
  40.       /*
  41.       ** Manipulate the stack so that the new node is on top, and
  42.       ** increment count.
  43.       */
  44.  
  45.       if ( pstack->ptop == NULL )
  46.       {
  47.             padd->pprev = NULL;   /* Anchor it!  */
  48.             pstack->ptop = padd;
  49.       }
  50.       else
  51.       {
  52.             padd->pprev = pstack->ptop;
  53.             pstack->ptop = padd;
  54.       }
  55.       pstack->vcount++;
  56.  
  57.       return (1);
  58. }
  59.  
  60. int stkPop(void **ppdata, Stk *pstack)
  61. {
  62.       StkNode *pdel;   /* Pointer to node to remove (top) */
  63.  
  64.       /*
  65.       ** Check to see if stack is empty.
  66.       */
  67.  
  68.       if ( pstack == NULL )
  69.       {
  70.             ppdata = NULL;
  71.             return (0);
  72.       }
  73.  
  74.       /*
  75.       ** Pop data pointer value into ppdata (pointer to user's pointer)
  76.       */
  77.  
  78.       *ppdata = pstack->ptop->pdata;
  79.  
  80.       /*
  81.       ** Pop the top of the stack off and make the next in line top.
  82.       ** Also, decrement counter.
  83.       */
  84.  
  85.       pdel = pstack->ptop;
  86.       pstack->ptop = pstack->ptop->pprev;
  87.       free(pdel);
  88.       pstack->vcount--;
  89.  
  90.       return (1);
  91. }
  92.  
  93. unsigned stkCount(Stk *pstack)
  94. {
  95.       return (pstack->vcount);
  96. }
  97.