home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch8 / stack.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  61 lines

  1. /*                         stack.c
  2.  *
  3.  *  This file contains the stack utility functions push(), pop(),
  4.  *  init_stack(), is_empty(), and is_full().
  5.  */
  6.  
  7. /* Include Files */
  8. #include "stack.h"                                   /* Note 1 */
  9.  
  10. /*******************************init_stack()********************/
  11.  
  12. void init_stack( STACK *s_ptr )                         /* Note 2 */
  13. {
  14.      s_ptr->top = MAX;                                 /* Note 3 */
  15. }
  16.  
  17. /*******************************is_empty()**********************/
  18.  
  19. int is_empty( STACK s )
  20. {
  21.      if ( s.top >= MAX )                             /* Note 4 */
  22.           return ( 1 );
  23.      else
  24.           return ( 0 );
  25. }
  26.  
  27. /*******************************is_full()***********************/
  28.  
  29. int is_full ( STACK s )
  30. {
  31.      if ( s.top <= 0 )                               /* Note 5 */
  32.           return ( 1 );
  33.      else
  34.           return ( 0 );
  35. }
  36.  
  37. /*******************************push()**************************/
  38.  
  39. int push( char item, STACK *s_ptr )
  40. {
  41.      if ( is_full( *s_ptr ) )                        /* Note 6 */
  42.           return ( -1 );
  43.      else {
  44.           s_ptr->top--;                                 /* Note 7 */
  45.           s_ptr->elts[ s_ptr->top ] = item;
  46.           return ( 0 );
  47.      }
  48. }
  49.  
  50. /*******************************pop()***************************/
  51.  
  52. int pop( STACK *s_ptr )
  53. {
  54.      if ( is_empty( *s_ptr ) )                        /* Note 8 */
  55.           return ( -1 );
  56.      else {
  57.                                                       /* Note 9 */
  58.           return ( s_ptr->elts[ s_ptr->top++ ] );
  59.      }
  60. }
  61.