home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / c / gnu_c / patches / DeskLib / h / LinkList < prev    next >
Encoding:
Text File  |  1994-10-03  |  5.1 KB  |  156 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    LinkList.h
  12.     Author:  Copyright © 1992 John Winters
  13.     Version: 1.33 (30 May 1994)
  14.     Purpose: Linked list management routines
  15. */
  16.  
  17.  
  18. #ifndef __dl_linklist_h
  19. #define __dl_linklist_h
  20.  
  21. #ifndef __dl_core_h
  22. #include "Core.h"
  23. #endif
  24.  
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28.  
  29. /*  Implementation notes
  30.  *  ====================
  31.  *  This module provides support for a linked list structure.
  32.  *  YOU must supply an anchor for the list (a variable of type
  33.  *  linklist_header). This is used to record pointers to the start and end
  34.  *  of the list.
  35.  *  The list itself is made up of your own structures, defined as follows:
  36.  *    struct listelement
  37.  *    {
  38.  *      linklist_header header;
  39.  *      ... Your own data here ...
  40.  *    } listelement;
  41.  *
  42.  *  a list-terminating pointer is represented as NULL
  43.  *
  44.  *  The header's next field stores a pointer to the FIRST item
  45.  *           its previous field stores a pointer to the LAST item
  46.  *           (both these pointers will be nulls if the list is empty)
  47.  *
  48.  *  To insert items into the list, you must malloc memory for one of your
  49.  *  own listelement data structures, then call the appropriate function
  50.  *  To delete an item from the list, call the unlink function, and then free
  51.  *  all memory used by your structure.
  52.  */
  53.  
  54.  
  55. typedef struct linklist_header
  56. {
  57.   struct linklist_header *next;
  58.   struct linklist_header *previous;
  59. } linklist_header ;
  60.  
  61.  
  62.  
  63.   /*  Add an item to a linked list- at head or tail of list.
  64.    *    AddToHead inserts the given item at the head of the list
  65.    *    AddToTail inserts the given item at the end of the list
  66.    *
  67.    *    InsertBefore inserts the given item BEFORE the item "pos"
  68.    *    InsertAfter inserts the given item AFTER the item "pos"
  69.    */
  70. extern void LinkList_AddToHead(linklist_header *anchor, linklist_header *item);
  71. extern void LinkList_AddToTail(linklist_header *anchor, linklist_header *item);
  72. extern void LinkList_InsertBefore(linklist_header *anchor,
  73.                                   linklist_header *pos,
  74.                                   linklist_header *item);
  75. extern void LinkList_InsertAfter(linklist_header *anchor,
  76.                                  linklist_header *pos,
  77.                                  linklist_header *item);
  78.  
  79.  
  80.   /*  LinkList_InList
  81.    *  Returns TRUE if the item is curently in the list. Note that this does
  82.    *  NOT check item contents, merely compares item pointers.
  83.    *  To check item contents, you must chain through the list yourself.
  84.    */
  85. extern BOOL LinkList_InList(linklist_header *anchor, linklist_header *item);
  86.  
  87.  
  88.   /*  LinkList_ListLength
  89.    *  Returns the number of items in the list. It follows all the links
  90.    *  and counts them... if you need this value a lot, I suggest you modify
  91.    *  LinkList to keep a count in the anchor...
  92.    */
  93. extern int LinkList_ListLength(linklist_header *anchor);
  94.  
  95.  
  96.  
  97.   /*  LinkList_Unlink
  98.    *  Unlinks the item "item" from the given list.
  99.    *  You must then free the memory used by it yourself.
  100.    */
  101. extern void LinkList_Unlink(linklist_header *anchor, linklist_header *item);
  102.  
  103.  
  104.   /*  Return pointers to the first/last item in the list
  105.    *    extern void *LinkList_FirstItem(linklist_header *anchor);
  106.    *    extern void *LinkList_LastItem(linklist_header *anchor);
  107.    */
  108. #define LinkList_FirstItem(x) ((void *)(x)->next)
  109. #define LinkList_LastItem(x) ((void *)(x)->previous)
  110.  
  111.  
  112.   /*  LinkList_Init
  113.    *  Initialises a list anchor. Should be called before anchor used
  114.    *    extern void LinkList_Init(linklist_header *item);
  115.    */
  116. #define LinkList_Init(x)  \
  117.   {                       \
  118.     (x)->next = NULL;     \
  119.     (x)->previous = NULL; \
  120.   }
  121.  
  122.  
  123.   /*  LinkList_InitItem
  124.    *  Initialises a list item. Should be called before item used,
  125.    *  except when that item is immediately linked into a list
  126.    *    extern void LinkList_InitItem(linklist_header *item);
  127.    */
  128. #define LinkList_InitItem(x) \
  129.   {                          \
  130.     (x)->next = NULL;        \
  131.     (x)->previous = NULL;    \
  132.   }
  133.  
  134.  
  135.   /*  Return the next/previous item of a given one. If the current item is at
  136.    *  the head/tail of the list, NULLPOINTER will be returned.
  137.    *    extern void *LinkList_NextItem(linklist_header *item);
  138.    *    extern void *LinkList_PreviousItem(linklist_header *item);
  139.    *
  140.    *  Old definitions:
  141.    *    #define LinkList_NextItem(x) ((void *)(x)->next)
  142.    *    #define LinkList_PreviousItem(x) ((void *)(x)->previous)
  143.    *
  144.    *  NEW definitions:
  145.    *    you can now use   LinkList_NextItem(&item);
  146.    *    instead of        LinkList_NextItem(&item->header);
  147.    */
  148. #define LinkList_NextItem(x) ((void *) ((linklist_header *)(x))->next)
  149. #define LinkList_PreviousItem(x) ((void *) ((linklist_header *)(x))->previous)
  150.  
  151. #ifdef __cplusplus
  152.            }
  153. #endif
  154.  
  155. #endif
  156.