home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / CONTAINR.ZIP / STACK.CPP < prev    next >
C/C++ Source or Header  |  1990-01-27  |  1KB  |  84 lines

  1. //  Module:     Stack    (A stack class)
  2. //  Version:    3.20
  3. //
  4. //  Language:   C++ 2.0
  5. //  Environ:    Any
  6. //
  7. //  Purpose:    Provides a a stack class for C++
  8. //
  9. //  Written by: Scott Robert Ladd
  10.  
  11. #include "Stack.hpp"
  12.  
  13. extern "C"
  14.     {
  15.     #include "stddef.h"
  16.     }
  17.  
  18. // constructor
  19. Stack::Stack() : SinglyLinkedList()
  20.     {}
  21.  
  22. // copy constructor
  23. Stack::Stack(const Stack & st) : SinglyLinkedList(st)
  24.     {}
  25.  
  26. // assignment operator
  27. void Stack::operator = (const Stack & st)
  28.     {
  29.     this->SinglyLinkedList::operator = (st);
  30.     }
  31.  
  32. // add new item
  33. int Stack::Store(void * item)
  34.     {
  35.     ListNode * new_node;
  36.  
  37.     new_node = new ListNode;
  38.  
  39.     if (NULL == new_node)
  40.         return 1;
  41.  
  42.     new_node->Next    = Head;
  43.     new_node->DataPtr = item;
  44.  
  45.     Head = new_node;
  46.  
  47.     if (Tail == NULL)
  48.         Tail = new_node;
  49.  
  50.     ++Count;
  51.  
  52.     return 0;
  53.     }
  54.  
  55. // examine the top item on the stack
  56. void * Stack::Examine()
  57.     {
  58.     if (Count == 0)
  59.         return NULL;
  60.  
  61.     return Head->DataPtr;
  62.     }
  63.  
  64. // read and remove the top item on the stack
  65. void * Stack::Retrieve()
  66.     {
  67.     ListNode * temp;
  68.     void *     value;
  69.  
  70.     if (Count == 0)
  71.         return NULL;
  72.  
  73.     value = Head->DataPtr;
  74.     temp  = Head->Next;
  75.  
  76.     delete Head;
  77.  
  78.     Head = temp;
  79.  
  80.     --Count;
  81.  
  82.     return value;
  83.     }
  84.