home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 7-1.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  733b  |  34 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. template <class T> class Stack;
  7.  
  8. template <class T> class Cell {
  9. friend class Stack<T>;
  10. private:
  11.     Cell *next;
  12.     T *rep;
  13.     Cell(T *r, Cell<T> *c): rep(r), next(c) { }
  14. };
  15.  
  16. template <class T> class Stack {
  17. public:
  18.     T *pop();
  19.     T *top() { return rep->rep; }
  20.     void push(T *v) { rep = new Cell<T>(v, rep); }
  21.     int empty() { return rep == 0; }
  22.     Stack() { rep = 0; }
  23. private:
  24.     Cell<T> *rep;
  25. };
  26.  
  27. template <class T> T *Stack<T>::pop() {
  28.     T *ret = rep->rep;
  29.     Cell<T> *c = rep;
  30.     rep = rep->next;
  31.     delete c;
  32.     return ret;
  33. }
  34.