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

  1. #ifndef _QUEUE_LIST_CODE
  2. #define _QUEUE_LIST_CODE
  3.  
  4. // Code written by Steven De Toni ACBC 11
  5.  
  6. // These class methods used to implement a object that holds other objects
  7. // that are decendant of ANYOBJECT (i.e universal container).
  8.  
  9. #include "baseq.h"
  10. #include <stdio.h>            // NULL CONSTANT
  11.  
  12.  
  13. // ############################################################################
  14. // #### QueueList Class ####
  15. // #########################
  16.  
  17. // ############################ Protected Methods #############################
  18.  
  19. // Create new list item and place programmer's data within it,
  20. // make new links with previous item if wished.
  21. // returns a pointer to the new item, NULL if operation failed.
  22. //
  23. // Parameters:
  24. //     pItem    : Pointer to the object to be stored.
  25. //     LinkItem : Pointer to end of list where item to be added.
  26. //
  27. // Return Values:
  28. //     LinkItem : returns pointer to newly added item in list,
  29. //                NULL if operation failed. However items within list
  30. //                before hand still exist.
  31. LinkItem* QueueList::newItem (ANYOBJECT* pItem, LinkItem* pEndList)
  32. {
  33.     LinkItem*  pNewStruct = new LinkItem;
  34.  
  35.     if (pNewStruct != NULL)
  36.         {
  37.         pNewStruct->pLinkedItem    = pEndList;
  38.         pNewStruct->pStoredItem    = pItem;
  39.         }
  40.  
  41.     return pNewStruct;
  42. }
  43.  
  44.  
  45. // ############################## Public Methods ##############################
  46. // ############################### Constructors ###############################
  47. QueueList::QueueList       (void)
  48. {
  49.     itemCount      = 0;
  50.     spaceAvailable = 0;
  51.     pEndPos        = NULL;
  52. }
  53.  
  54.  
  55. // Parameters:
  56. //     pItem    : Pointer to a object to be stored, must be decendant of
  57. //                base class ANYOBJECT.
  58. QueueList::QueueList       (ANYOBJECT* pItem)
  59. {
  60.     itemCount      = 0;
  61.     spaceAvailable = 0;
  62.     pEndPos        = NULL;
  63.     putLast(pItem);
  64. }
  65.  
  66.  
  67. // ########################### User Methods ###################################
  68.  
  69. // Place programmers object into list
  70. //
  71. // Parameters:
  72. //     pItem    : Pointer to a object to be stored, must be decendant of
  73. //                base class ANYOBJECT.
  74. //
  75. // Return Values:
  76. //     int      : Returns a error code indicating whether operation was
  77. //                successful.
  78. //                Values:
  79. //                    0 = No Worries
  80. //                   -1 = Arrgh ... No memory
  81. //
  82. int       QueueList::putLast   (ANYOBJECT* pItem)
  83. {
  84.     LinkItem*   pNewItem = newItem (pItem, pEndPos);
  85.     if (pNewItem != NULL)
  86.         {
  87.         pEndPos = pNewItem;
  88.         itemCount++;
  89.         return 0;
  90.         }
  91.     spaceAvailable = -1;
  92.     return -1;                // could not add item to list!
  93. }
  94.  
  95.  
  96. // Take first item placed in Queue, out and return it.
  97. // Type casting is required to return object back to it's orginial
  98. // state.
  99. //
  100. // Return Values:
  101. //     ANYOBJECT* : Pointer to the object that was stored within queue.
  102. //
  103. ANYOBJECT*   QueueList::takeNext         (void)
  104. {
  105.     if (pEndPos != NULL)
  106.         {
  107.         LinkItem* pUpDateList = pEndPos;
  108.         LinkItem* pStartPos   = pEndPos;
  109.  
  110.         // move down list until start has been reached
  111.         while (pStartPos->pLinkedItem != NULL)
  112.             pStartPos = pStartPos->pLinkedItem;
  113.  
  114.                               // if not the last item in list
  115.         if (pStartPos != pUpDateList)
  116.             {
  117.             // retrieve data and delete item from list
  118.             while (pUpDateList->pLinkedItem != pStartPos)
  119.                 pUpDateList = pUpDateList->pLinkedItem;
  120.             }
  121.         else
  122.             {
  123.             pEndPos = NULL;   // start new list after all items gone
  124.             }
  125.  
  126.                               // copy value to user
  127.         ANYOBJECT* pTemp = pStartPos->pStoredItem;
  128.                               // make new start of list
  129.         pUpDateList->pLinkedItem = NULL;
  130.         delete pStartPos;     // delete object
  131.         itemCount--;          // one less
  132.         if (spaceAvailable)   // if no memory available before...
  133.                               // there is now!
  134.                 spaceAvailable = 0;
  135.         return pTemp;
  136.         }
  137.     else
  138.         return NULL;
  139. }
  140.  
  141.  
  142. // Returns the number of items contained within the queue.
  143. //
  144. // Returns Values:
  145. //     int :    Num of items within queue.
  146. //
  147.                               // return number of item in Queue
  148. int      QueueList::status (void)
  149. {
  150.     return itemCount;
  151. }
  152.  
  153.  
  154. // Method returns whether last operation failed due to memory allocation
  155. // failure.
  156. //
  157. // Return Values:
  158. //     int  : Returns 1 of two values ...
  159. //            Values:
  160. //              0  =  memory available
  161. //             -1  =  Last memory allocation failed.
  162. //
  163.                               // return Queue space left
  164. int      QueueList::space  (void)
  165. {
  166.     return spaceAvailable;    // return -1 if no space available
  167. }
  168.  
  169.  
  170. // Methods is used to peek within the queue at objects, and return there
  171. // pointer without taking them out of the queue.
  172. //
  173. // Parameters:
  174. //     NumFromNext : The object number to look at from the start of the
  175. //                   queue. The start of the queue is 1, not 0.
  176. //
  177. // Return Values:
  178. //     ANYOBJECT* : Pointer to the object that is stored within queue,
  179. //                  at said position. Returns NULL if operation failed.
  180. //
  181. ANYOBJECT*   QueueList::peek (int numFromNext)
  182. {
  183.     if (pEndPos != NULL)
  184.         {
  185.         //if (numFromNext > itemCount) //error checking !
  186.         //   return NULL;
  187.  
  188.         int count = itemCount - numFromNext;
  189.         LinkItem* pStartPos   = pEndPos;
  190.  
  191.         // move down list until start has been reached
  192.         while (count > 0)
  193.             {
  194.             pStartPos = pStartPos->pLinkedItem;
  195.             count--;
  196.             }
  197.  
  198.         if (pStartPos != NULL)
  199.             return pStartPos->pStoredItem;
  200.         else
  201.             return NULL;
  202.         }
  203.     else
  204.         return NULL;
  205. }
  206.  
  207.  
  208. // ############################### Destructor ###############################
  209. // Method will remove all list items from memory if they still exist,
  210. // no garabage collection provided, or used.
  211. //
  212. QueueList::~QueueList      (void)
  213. {
  214.     LinkItem* pTemp = pEndPos;
  215.  
  216.     while (pEndPos != NULL)
  217.         {
  218.                               // advance to next item
  219.         pEndPos = pEndPos->pLinkedItem;
  220.                               // kill data contained
  221.         delete    pTemp  ->pStoredItem;
  222.         delete    pTemp;      // kill item
  223.         pTemp   = pEndPos;
  224.         }
  225. }
  226.  
  227.  
  228. #endif
  229.  
  230.