home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / toolkt21 / c / samples / tp / ll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-12  |  9.1 KB  |  305 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)ll.c 1.3 1/22/92 16:10:02 [1/26/92] (c)IBM Corp. 1992";
  3. #endif
  4.  
  5. /*
  6.  * This class is adapted from the book
  7.  *   Class Construction in C and C++, Object Oriented Fundamentals
  8.  *   by Roger Sessions, Copyright (c) 1992 Prentice Hall.
  9.  * Reprinted with permission.
  10.  */
  11.  
  12. #define linkedList_Class_Source
  13. #include "link.h"
  14. #include "ll.ih"
  15. #define MAX_INT 30000
  16.  
  17. /* ************************************************************ */
  18. SOM_Scope baseType *SOMLINK llHead(linkedList * somSelf)
  19. {
  20.     linkedListData *somThis = linkedListGetData(somSelf);
  21.     linkedListMethodDebug("linkedList", "llHead");
  22.  
  23.     if (!_nlinks)
  24.     return (baseType *) NULL;
  25.     _currentLink = _headLink;
  26.     return (_linkGetContents(_currentLink));
  27. }
  28.  
  29. /* ************************************************************ */
  30. SOM_Scope baseType *SOMLINK llTail(linkedList * somSelf)
  31. {
  32.     linkedListData *somThis = linkedListGetData(somSelf);
  33.     linkedListMethodDebug("linkedList", "llTail");
  34.  
  35.     if (!_nlinks)
  36.     return (baseType *) NULL;
  37.     _currentLink = _tailLink;
  38.     return (_linkGetContents(_currentLink));
  39. }
  40.  
  41. /* ************************************************************ */
  42. SOM_Scope int SOMLINK llLength(linkedList * somSelf)
  43. {
  44.     linkedListData *somThis = linkedListGetData(somSelf);
  45.     linkedListMethodDebug("linkedList", "llLength");
  46.     return _nlinks;
  47. }
  48.  
  49. /* ************************************************************ */
  50. SOM_Scope void SOMLINK llSetMax(linkedList * somSelf,
  51.                  int newMax)
  52. {
  53.     linkedListData *somThis = linkedListGetData(somSelf);
  54.     linkedListMethodDebug("linkedList", "llSetMax");
  55.     _max = newMax;
  56. }
  57.  
  58. /* ************************************************************ */
  59. SOM_Scope int SOMLINK llLeft(linkedList * somSelf)
  60. {
  61.     linkedListData *somThis = linkedListGetData(somSelf);
  62.     linkedListMethodDebug("linkedList", "llLeft");
  63.     return (_max - _llLength(somSelf));
  64. }
  65.  
  66. /* ************************************************************ */
  67. SOM_Scope baseType *SOMLINK llNext(linkedList * somSelf)
  68. {
  69.     linkedListData *somThis = linkedListGetData(somSelf);
  70.     linkedListMethodDebug("linkedList", "llNext");
  71.  
  72.     if (!_nlinks)
  73.     return (baseType *) NULL;
  74.     if (_linkGetNext(_currentLink)) {
  75.     _currentLink = _linkGetNext(_currentLink);
  76.     return (_linkGetContents(_currentLink));
  77.     }
  78.     else {
  79.     return (baseType *) NULL;
  80.     }
  81. }
  82.  
  83. /* ************************************************************ */
  84. SOM_Scope baseType *SOMLINK llPrevious(linkedList * somSelf)
  85. {
  86.     linkedListData *somThis = linkedListGetData(somSelf);
  87.     linkedListMethodDebug("linkedList", "llPrevious");
  88.  
  89.     if (!_nlinks)
  90.     return (baseType *) NULL;
  91.     if (_linkGetPrevious(_currentLink)) {
  92.     _currentLink = _linkGetPrevious(_currentLink);
  93.     return (_linkGetContents(_currentLink));
  94.     }
  95.     else {
  96.     return (baseType *) NULL;
  97.     }
  98. }
  99.  
  100. /* ************************************************************ */
  101. SOM_Scope baseType *SOMLINK llRetrieve(linkedList * somSelf)
  102. {
  103.     linkedListData *somThis = linkedListGetData(somSelf);
  104.     linkedListMethodDebug("linkedList", "llRetrieve");
  105.  
  106.     if (!_nlinks)
  107.     return (baseType *) NULL;
  108.     return (_linkGetContents(_currentLink));
  109. }
  110.  
  111. /* ************************************************************ */
  112. SOM_Scope baseType *SOMLINK llReplace(linkedList * somSelf,
  113.                        baseType * newElement)
  114. {
  115.     linkedListData *somThis = linkedListGetData(somSelf);
  116.     linkedListMethodDebug("linkedList", "llReplace");
  117.  
  118.     if (!_nlinks)
  119.     return (baseType *) NULL;
  120.     _linkSetContents(_currentLink, newElement);
  121.     return (_linkGetContents(_currentLink));
  122. }
  123.  
  124. /* ************************************************************ */
  125. SOM_Scope baseType *SOMLINK llPromoteTail(linkedList * somSelf)
  126. {
  127.     linkedListData *somThis = linkedListGetData(somSelf);
  128.     link *oldTail;
  129.     linkedListMethodDebug("linkedList", "llPromoteTail");
  130.     if (!_nlinks)
  131.     return (baseType *) NULL;
  132.     if (_nlinks == 1)
  133.     return (_linkGetContents(_headLink));
  134.     oldTail = _tailLink;
  135.     _tailLink = _linkGetPrevious(_tailLink);
  136.     _linkSetNext(_linkGetPrevious(oldTail), 0);
  137.     _linkSetPrevious(oldTail, 0);
  138.     _linkSetNext(oldTail, _headLink);
  139.     _linkSetPrevious(_headLink, oldTail);
  140.     _headLink = oldTail;
  141.     _currentLink = _headLink;
  142. }
  143.  
  144. /* ************************************************************ */
  145. SOM_Scope baseType *SOMLINK llAddHead(linkedList * somSelf,
  146.                        baseType * newElement)
  147. {
  148.     linkedListData *somThis = linkedListGetData(somSelf);
  149.     link *newLink = linkNew();
  150.     linkedListMethodDebug("linkedList", "llAddHead");
  151.  
  152.     _linkSetContents(newLink, newElement);
  153.     if (!_llLeft(somSelf)) {
  154.     _llHead(somSelf);
  155.     return (_llReplace(somSelf, newElement));
  156.     }
  157.     if (_llHead(somSelf)) {
  158.     _linkSetPrevious(_currentLink, newLink);
  159.     }
  160.     else
  161.     _tailLink = newLink;
  162.     _linkSetNext(newLink, _currentLink);
  163.     _headLink = _currentLink = newLink;
  164.     _nlinks++;
  165.     return (_linkGetContents(_currentLink));
  166. }
  167.  
  168. /* ************************************************************ */
  169. SOM_Scope baseType *SOMLINK llAddTail(linkedList * somSelf,
  170.                        baseType * newElement)
  171. {
  172.     linkedListData *somThis = linkedListGetData(somSelf);
  173.     link *newLink = linkNew();
  174.     linkedListMethodDebug("linkedList", "llAddTail");
  175.  
  176.     _linkSetContents(newLink, newElement);
  177.     if (!_llLeft(somSelf)) {
  178.     _llTail(somSelf);
  179.     return (_llReplace(somSelf, newElement));
  180.     }
  181.     if (_llTail(somSelf)) {
  182.     _linkSetNext(_currentLink, newLink);
  183.     }
  184.     else
  185.     _headLink = newLink;
  186.     _linkSetPrevious(newLink, _currentLink);
  187.     _tailLink = _currentLink = newLink;
  188.     _nlinks++;
  189.     return (_linkGetContents(_currentLink));
  190. }
  191.  
  192. /* ************************************************************ */
  193. SOM_Scope baseType *SOMLINK llRemoveHead(linkedList * somSelf)
  194. {
  195.     linkedListData *somThis = linkedListGetData(somSelf);
  196.     baseType *thisItem;
  197.     linkedListMethodDebug("linkedList", "llRemoveHead");
  198.  
  199.     if (!_nlinks)
  200.     return (baseType *) NULL;
  201.     thisItem = _llHead(somSelf);
  202.     if (_nlinks == 1) {
  203.     _somFree(_headLink);
  204.     _headLink = _tailLink = _currentLink = 0;
  205.     }
  206.     if (_nlinks > 1) {
  207.     _llNext(somSelf);
  208.     _somFree(_headLink);
  209.     _headLink = _currentLink;
  210.     _linkSetPrevious(_headLink, 0);
  211.     }
  212.     _nlinks--;
  213.     return thisItem;
  214. }
  215.  
  216. /* ************************************************************ */
  217. SOM_Scope int SOMLINK llIsTail(linkedList * somSelf)
  218. {
  219.     linkedListData *somThis = linkedListGetData(somSelf);
  220.     linkedListMethodDebug("linkedList", "llIsTail");
  221.     return (_currentLink == _tailLink);
  222.  
  223. }
  224.  
  225. /* ************************************************************ */
  226. SOM_Scope void SOMLINK llFreeContents(linkedList * somSelf)
  227. {
  228.     linkedListData *somThis = linkedListGetData(somSelf);
  229.     baseType *thisItem;
  230.  
  231.     linkedListMethodDebug("linkedList", "llFreeContents");
  232.  
  233.     while (thisItem = _llRemoveHead(somSelf))
  234.     _somFree(thisItem);
  235. }
  236.  
  237. /* ************************************************************ */
  238. SOM_Scope void SOMLINK print(linkedList * somSelf,
  239.                   FILE * outputFile)
  240. {
  241.     linkedListData *somThis = linkedListGetData(somSelf);
  242.     baseType *thisItem;
  243.  
  244.     linkedListMethodDebug("linkedList", "print");
  245.     thisItem = _llHead(somSelf);
  246.     if (thisItem) {
  247.     while (thisItem) {
  248.         _print(thisItem, outputFile);
  249.         thisItem = _llNext(somSelf);
  250.     }
  251.     }
  252. }
  253.  
  254. /* ************************************************************ */
  255. SOM_Scope void SOMLINK somInit(linkedList * somSelf)
  256. {
  257.     linkedListData *somThis = linkedListGetData(somSelf);
  258.     linkedListMethodDebug("linkedList", "somInit");
  259.     parent_somInit(somSelf);
  260.  
  261.     _currentLink = 0;
  262.     _headLink = 0;
  263.     _tailLink = 0;
  264.     _nlinks = 0;
  265.     _max = MAX_INT;
  266. }
  267.  
  268. /* ************************************************************ */
  269. SOM_Scope void SOMLINK somUninit(linkedList * somSelf)
  270. {
  271.     linkedListData *somThis = linkedListGetData(somSelf);
  272.     linkedListMethodDebug("linkedList", "somUninit");
  273.  
  274.     while (_llRemoveHead(somSelf))
  275.     ;
  276.     parent_somUninit(somSelf);
  277. }
  278.  
  279. /* ************************************************************ */
  280. SOM_Scope void SOMLINK somDumpSelfInt(linkedList * somSelf,
  281.                        int level)
  282. {
  283.     linkedListData *somThis = linkedListGetData(somSelf);
  284.     linkedListMethodDebug("linkedList", "somDumpSelfInt");
  285.     parent_somDumpSelfInt(somSelf, level);
  286. }
  287.  
  288. /* ************************************************************ */
  289. SOM_Scope void SOMLINK llTrace(linkedList * somSelf, FILE * output)
  290. {
  291.     linkedListData *somThis = linkedListGetData(somSelf);
  292.     linkedListMethodDebug("linkedList", "llTrace");
  293.     fprintf(output, "\n");
  294.     fprintf(output, " Linked List\n");
  295.     fprintf(output, "        max: %d\n", _max);
  296.     fprintf(output, "     nlinks: %d\n", _nlinks);
  297.     fprintf(output, "currentLink: %x\n", _currentLink);
  298.     fprintf(output, "   headLink: %x\n", _headLink);
  299.     fprintf(output, "   tailLink: %x\n", _tailLink);
  300.  
  301.     _print(_headLink, output);
  302.     _print(_currentLink, output);
  303.     _print(_tailLink, output);
  304. }
  305.