home *** CD-ROM | disk | FTP | other *** search
/ ftptest.leeds.ac.uk / 2015.02.ftptest.leeds.ac.uk.tar / ftptest.leeds.ac.uk / bionet / CAE-GROUP / SCL-WIN3x / FED_PLUS.EXE / LINKLIST.C < prev    next >
C/C++ Source or Header  |  1994-07-25  |  4KB  |  235 lines

  1. static char rcsid[] = "$Id: linklist.c,v 1.2 1993/10/15 18:49:55 libes Exp $";
  2.  
  3. /*
  4.  * This work was supported by the United States Government, and is
  5.  * not subject to copyright.
  6.  *
  7.  * $Log: linklist.c,v $
  8.  * Revision 1.2  1993/10/15  18:49:55  libes
  9.  * CADDETC certified
  10.  *
  11.  * Revision 1.4  1993/01/19  22:17:27  libes
  12.  * *** empty log message ***
  13.  *
  14.  * Revision 1.3  1992/08/18  17:16:22  libes
  15.  * rm'd extraneous error messages
  16.  *
  17.  * Revision 1.2  1992/06/08  18:08:05  libes
  18.  * prettied up interface to print_objects_when_running
  19.  */
  20.  
  21. #define LINKLIST_C
  22. #include "linklist.h"
  23.  
  24. void
  25. LISTinitialize(void)
  26. {
  27.     MEMinitialize(&LINK_fl,sizeof(struct Link),500,100);
  28.     MEMinitialize(&LIST_fl,sizeof(struct Linked_List),100,50);
  29.  
  30.     ERROR_empty_list = ERRORcreate(
  31. "Empty list in %s.", SEVERITY_ERROR);
  32.  
  33. }
  34.  
  35. Boolean
  36. LISTempty(Linked_List list)
  37. {
  38.     if (!list) return true;
  39.     return (list->mark->next == list->mark);
  40. }
  41.  
  42. Linked_List
  43. LISTcreate()
  44. {
  45.     Linked_List list = LIST_new();
  46.     list->mark = LINK_new();
  47.     list->mark->next = list->mark->prev = list->mark;
  48.     return(list);
  49. }
  50.  
  51. #if 0
  52. /* could optimize this function! */
  53. Linked_List
  54. LISTcreate_with(Generic g)
  55. {
  56.     Linked_List dst = LISTcreate();
  57.     LISTadd(dst,g);
  58.     return(dst);
  59. }
  60. #endif
  61.  
  62. Linked_List
  63. LISTcopy(Linked_List src)
  64. {
  65.     Linked_List dst = LISTcreate();
  66.     LISTdo(src,x,Generic)
  67.         LISTadd(dst,x);
  68.     LISTod
  69.     return dst;
  70. }
  71.     
  72.  
  73. void
  74. LISTfree(list)
  75. Linked_List list;
  76. {
  77.     Link p, q = list->mark->next;
  78.  
  79.     for (p = q->next; p != list->mark; q = p, p = p->next) {
  80.             LINK_destroy(q);
  81.     }
  82.     if (q != list->mark) LINK_destroy(q);
  83.     LINK_destroy(list->mark);
  84.     LIST_destroy(list);
  85. }
  86.  
  87. Generic
  88. LISTadd_first(Linked_List list, Generic item)
  89. {
  90.     Link        node;
  91.  
  92.     node = LINK_new();
  93.     node->data = item;
  94.     (node->next = list->mark->next)->prev = node;
  95.     (list->mark->next = node)->prev = list->mark;
  96.     return item;
  97. }
  98.  
  99. Generic
  100. LISTadd_last(Linked_List list, Generic item)
  101. {
  102.     Link        node;
  103.  
  104.     node = LINK_new();
  105.     node->data = item;
  106.     (node->prev = list->mark->prev)->next = node;
  107.     (list->mark->prev = node)->next = list->mark;
  108.     return item;
  109. }
  110.  
  111. Generic
  112. LISTadd_after(Linked_List list, Link link, Generic item)
  113. {
  114.     Link node;
  115.  
  116.     if (link == LINK_NULL)
  117.     LISTadd_first(list, item);
  118.     else {
  119.     node = LINK_new();
  120.     node->data = item;
  121.     (node->next = link->next)->prev = node;
  122.     (link->next = node)->prev = link;
  123.     }
  124.     return item;
  125. }
  126.  
  127. Generic
  128. LISTadd_before(Linked_List list, Link link, Generic item)
  129. {
  130.     Link node;
  131.  
  132.     if (link == LINK_NULL)
  133.     LISTadd_last(list, item);
  134.     else {
  135.     node = LINK_new();
  136.     node->data = item;
  137.  
  138.     link->prev->next = node;    /* fix up previous link */
  139.     node->prev = link->prev;
  140.     node->next = link;
  141.     link->prev = node;        /* fix up next link */
  142.     }
  143.     return item;
  144. }
  145.  
  146.  
  147. Generic
  148. LISTremove_first(Linked_List list)
  149. {
  150.     Link        node;
  151.     Generic        item;
  152.  
  153.     node = list->mark->next;
  154.     if (node == list->mark) {
  155.     ERRORreport(ERROR_empty_list, "LISTremove_first");
  156.     return NULL;
  157.     }
  158.     item = node->data;
  159.     (list->mark->next = node->next)->prev = list->mark;
  160.     LINK_destroy(node);
  161.     return item;
  162. }
  163.  
  164. /* 1st arg is historical and can be removed */
  165. /*ARGSUSED*/
  166. Generic
  167. LISTremove(Linked_List list, Link link)
  168. {
  169.     Generic        item;
  170.  
  171.     link->next->prev = link->prev;
  172.     link->prev->next = link->next;
  173.     item = link->data;
  174.     LINK_destroy(link);
  175.     return item;
  176. }
  177.  
  178. Generic
  179. LISTget_first(Linked_List list)
  180. {
  181.     Link node;
  182.     Generic item;
  183.  
  184.     node = list->mark->next;
  185.     if (node == list->mark) {
  186.     return NULL;
  187.     }
  188.     item = node->data;
  189.     return item;
  190. }
  191. Generic
  192. LISTget_second(Linked_List list)
  193. {
  194.     Link        node;
  195.     Generic        item;
  196.  
  197.     node = list->mark->next;
  198.     if (node == list->mark) {
  199.     return NULL;
  200.     }
  201.     node = node->next;
  202.     if (node == list->mark) {
  203.     return NULL;
  204.     }
  205.     item = node->data;
  206.     return item;
  207. }
  208.  
  209. /* first is 1, not 0 */
  210. Generic
  211. LISTget_nth(Linked_List list, int n)
  212. {
  213.     int count = 1;
  214.     Link node;
  215.  
  216.     for (node = list->mark->next; node != list->mark; node = node->next) {
  217.         if (n == count++) return(node->data);
  218.     }
  219.     return(0);
  220. }
  221.  
  222. int
  223. LISTget_length(Linked_List list)
  224. {
  225.     Link node;
  226.     int count = 0;
  227.  
  228.     if (!list) return 0;
  229.  
  230.     for (node = list->mark->next; node != list->mark; node = node->next) {
  231.         count++;
  232.     }
  233.     return count;
  234. }
  235.