home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / STACK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  600 b   |  39 lines

  1. // stack.cpp:   Implementation of the Stack class
  2. // from Chapter 6 of Getting Started
  3. #include <iostream.h>
  4. #include "stack.h"
  5.  
  6. int Stack::push(int elem)
  7. {
  8.    int m = getmax();
  9.    if (top < m)
  10.    {
  11.       put_elem(elem,top++);
  12.       return 0;
  13.    }
  14.    else
  15.       return -1;
  16. }
  17.  
  18. int Stack::pop(int& elem)
  19. {
  20.    if (top > 0)
  21.    {
  22.       get_elem(elem,--top);
  23.       return 0;
  24.    }
  25.    else
  26.       return -1;
  27. }
  28.  
  29. void Stack::print()
  30. {
  31.    int elem;
  32.  
  33.    for (int i = top-1; i >= 0; --i)
  34.    {  // Print in LIFO order
  35.       get_elem(elem,i);
  36.       cout << elem << "\n";
  37.    }
  38. }
  39.