home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 2-2A.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  695b  |  46 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #define STACK_SIZE 10
  7.  
  8. struct Stack {
  9.     long items[STACK_SIZE];
  10.     int sp;
  11. };
  12.  
  13. void Stack_initialize(s)
  14. struct Stack *s;
  15. {
  16.     s->sp = -1;
  17. }
  18.  
  19. long Stack_top(s)
  20. struct Stack *s;
  21. {
  22.     return s->items[s->sp];
  23. }
  24.  
  25. long Stack_pop(s)
  26. struct Stack *s;
  27. {
  28.     return s->items[s->sp--];
  29. }
  30.  
  31. void Stack_push(s, i)
  32. struct Stack *s; long i;
  33. {
  34.     s->items[++s->sp] = i;
  35. }
  36.  
  37. int main()
  38. {
  39.     struct Stack q;
  40.     int i;
  41.     Stack_initialize(&q);
  42.     Stack_push(&q,1);
  43.     i = Stack_top(&q);
  44.     Stack_pop(&q);
  45. }
  46.