home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bcpp1611.zip / BCPP / STACKLIS.CPP < prev    next >
C/C++ Source or Header  |  1996-12-18  |  4KB  |  168 lines

  1. #ifndef _STACK_LIST_CODE
  2. #define _STACK_LIST_CODE
  3. // Code written by Steven De Toni ACBC 11
  4. // This file contains the methods that were defined in stacklist.h
  5. // header file (i.e container class that stores items in a linked list,
  6. // in stack form)
  7. #include "stacklis.h"
  8. #include <stdio.h>            // NULL Constant
  9. // ############################################################################
  10. // #### StackList Class ####
  11. // #########################
  12. // ############################## Public Methods ##############################
  13. // ############################### Constructors ###############################
  14. // Initialise internal variables.
  15. //
  16. StackList::StackList (void)
  17. {
  18.     pCurrentPos    = NULL;
  19.     itemCount      = 0;
  20.     spaceAvailable = 0;       // space is available
  21. }
  22.  
  23.  
  24. // Initalise vaiables, and place item passed in a new list
  25. //
  26. // Parameters:
  27. //     pItem    : Pointer to the object that is will to be stored.
  28. //                Item must be of desendant ANYOBJECT.
  29. //
  30. StackList::StackList (ANYOBJECT* pItem)
  31. {
  32.     pCurrentPos    = NULL;
  33.     itemCount      = 0;
  34.     spaceAvailable = 0;       // space is available
  35.     push (pItem);
  36. }
  37.  
  38.  
  39. // Places a new item in the list (i.e on the stack).
  40. //
  41. // Parameters:
  42. //     pItem    : Pointer to the object that is will to be stored.
  43. //                Item must be of desendant ANYOBJECT.
  44. //
  45. // Return Values:
  46. //     int      : Returns a error code value to indicate whether operation
  47. //                was successful or not.
  48. //                Value:
  49. //                0  =  No Worries, item stacked.
  50. //               -1  =  Item not stacked, memory allocation failure
  51. //
  52. int StackList::push (ANYOBJECT* pItem)
  53. {
  54.     ListLink*    newItem = new ListLink;
  55.     if (newItem)              // not NULL
  56.         {
  57.         // update contents of structure
  58.         newItem->pItem       = pItem;
  59.         newItem->pLinkedItem = NULL;
  60.         if (pCurrentPos)      // not NULL
  61.             newItem->pLinkedItem = pCurrentPos;
  62.         // update start of stack pointer
  63.         pCurrentPos = newItem;
  64.         itemCount++;
  65.         return spaceAvailable;
  66.         }
  67.     else
  68.         {
  69.         spaceAvailable = -1;
  70.         return  spaceAvailable;
  71.         }
  72. }
  73.  
  74.  
  75. // Removes a item from the list and returns the value contained within it
  76. // back to the user. A NULL value is returns if there are no more items
  77. // within the list.
  78. //
  79. // Return Values:
  80. //     ANYOBJECT* : Pointer to the object last object that was placed
  81. //                  on the stack. Returns NULL pointer if operation
  82. //                  failed.
  83. //
  84. ANYOBJECT* StackList::pop (void)
  85. {
  86.     ListLink*    pBackUp = pCurrentPos;
  87.     if (pCurrentPos)          // not NULL
  88.         {
  89.         ANYOBJECT* pTemp = pCurrentPos->pItem;
  90.         pCurrentPos      = pCurrentPos->pLinkedItem;
  91.         delete pBackUp;
  92.         itemCount--;
  93.         return pTemp;
  94.         }
  95.     else
  96.         return NULL;
  97. }
  98.  
  99.  
  100. // Peeks at items within the linked list without removing
  101. // them from the list.
  102. //
  103. // Parameters:
  104. //    int item :     item number in list.
  105. //
  106. // Return Values:
  107. //   ANYOBJECT*  : Returns NULL if operation failed, else
  108. //                 pointer to the object contained at list
  109. //                 number selected!
  110. //
  111. ANYOBJECT* StackList::peek (int item)
  112. {
  113.     ListLink*   pPeekPos = pCurrentPos;
  114.     // invalid range !
  115.     if ( ((item < 1) || (item > itemCount)) || (pPeekPos == NULL) )
  116.         return NULL;
  117.     while (item > 1)
  118.         {
  119.         pPeekPos = pPeekPos -> pLinkedItem;
  120.         item--;
  121.         }
  122.     return pPeekPos -> pItem;
  123. }
  124.  
  125.  
  126. // Method returns whether last operation failed due to memory allocation
  127. // failure.
  128. //
  129. // Return Values:
  130. //     int  : Returns 1 of two values ...
  131. //            Values:
  132. //              0  =  memory available
  133. //             -1  =  Last memory allocation failed.
  134. //
  135. int  StackList::space (void)
  136. {
  137.     return spaceAvailable;
  138. }
  139.  
  140.  
  141. // Returns the number of items current being stacked.
  142. //
  143. // Returns Values:
  144. //     int :    Num of items within queue.
  145. //
  146. int  StackList::status (void)
  147. {
  148.     return itemCount;
  149. }
  150.  
  151.  
  152. // ############################### Destructor ###############################
  153. // Method will remove all list items from memory if they still exist,
  154. // no garabage collection provided, or used.
  155. //
  156. StackList::~StackList  (void)
  157. {
  158.     ANYOBJECT* pTest = pop();
  159.     while (pTest != NULL)
  160.         {
  161.         delete pTest;
  162.         pTest = pop();
  163.         }
  164. }
  165.  
  166.  
  167. #endif
  168.