inc/dlist.h

Go to the documentation of this file.
00001 /* dlist.h - Headers for generic doubly-linked list routines. 
00002  *
00003  * This file is copyright 2002 Jim Kent, but license is hereby
00004  * granted for all use - public, private or commercial. */
00005 
00006 #ifndef DLIST_H
00007 #define DLIST_H
00008 
00009 #ifndef COMMON_H
00010 #include "common.h"
00011 #endif
00012 
00013 struct dlNode
00014 /* An element on a doubly linked list. */
00015     {
00016     struct dlNode *next;
00017     struct dlNode *prev;
00018     void *val;
00019     };
00020 
00021 struct dlList
00022 /* A doubly linked list. */
00023     {
00024     struct dlNode *head;
00025     struct dlNode *nullMiddle;
00026     struct dlNode *tail;
00027     };
00028 
00029 #define dlEnd(node) (node->next == NULL)
00030 /* True if node past end. */
00031 
00032 #define dlStart(node) (node->prev == NULL)
00033 /* True if node before start. */
00034 
00035 /* Iterate on a doubly linked list as so:
00036     for (el = list->head; !dlEnd(el); el = el->next)
00037         val = el->val;
00038    or
00039     for (el = list->tail; !dlStart(el); el = el->prev)
00040         val = el->val;
00041  */
00042 
00043 struct dlList *newDlList();
00044 /* Return a new doubly linked list. */
00045 
00046 #define dlListNew newDlList
00047 /* Add object-first synonym. */
00048 
00049 void dlListInit(struct dlList *dl);
00050 /* Initialize list to be empty */
00051 
00052 void dlListReset(struct dlList *dl);
00053 /* Reset a list to the empty state (does not free values)  */
00054 
00055 void freeDlList(struct dlList **pList);
00056 /* Free up a doubly linked list and it's nodes (but not the node values). */
00057 #define dlListFree freeDlList
00058 
00059 void freeDlListAndVals(struct dlList **pList);
00060 /* Free all values in doubly linked list and the list itself.  (Just calls
00061  * freeMem on all values. */
00062 #define dlListFreeAndVals freeDlListAndVals
00063 
00064 void dlAddBefore(struct dlNode *anchor, struct dlNode *newNode);
00065 /* Add a node to list before anchor member. */
00066 
00067 void dlAddAfter(struct dlNode *anchor, struct dlNode *newNode);
00068 /* Add a node to list after anchor member. */
00069 
00070 void dlAddHead(struct dlList *list, struct dlNode *newNode);
00071 /* Add a node to head of list. */
00072 
00073 void dlAddTail(struct dlList *list, struct dlNode *newNode);
00074 /* Add a node to tail of list. */
00075 
00076 struct dlNode *dlAddValBefore(struct dlNode *anchor, void *val);
00077 /* Create a node containing val and add to list before anchor member. */
00078 
00079 struct dlNode *dlAddValAfter(struct dlNode *anchor, void *val);
00080 /* Create a node containing val and add to list after anchor member. */
00081 
00082 struct dlNode *dlAddValHead(struct dlList *list, void *val);
00083 /* Create a node containing val and add to head of list. */
00084 
00085 struct dlNode *dlAddValTail(struct dlList *list, void *val);
00086 /* Create a node containing val and add to tail of list. */
00087 
00088 void dlRemove(struct dlNode *node);
00089 /* Removes a node from list. Node is not freed. */
00090 
00091 void dlRemoveHead(struct dlList *list);
00092 /* Removes head from list. Node is not freed. */
00093 
00094 void dlRemoveTail(struct dlList *list);
00095 /* Remove tail from list. Node is not freed.  */
00096 
00097 struct dlNode *dlPopHead(struct dlList *list);
00098 /* Remove first node from list and return it. */
00099 
00100 struct dlNode *dlPopTail(struct dlList *list);
00101 /* Remove last node from list and return it. */
00102 
00103 void dlDelete(struct dlNode **nodePtr);
00104 /* Removes a node from list and frees it. */
00105 
00106 int dlCount(struct dlList *list);
00107 /* Return length of list. */
00108 
00109 boolean dlEmpty(struct dlList *list);
00110 /* Return TRUE if list is empty. */
00111 
00112 #define dlIsEmpty(list) ((list)->head->next == NULL)
00113 /* Return TRUE if list is empty.  Macro version of above. */
00114 
00115 struct dlNode *dlGetBeforeHead(struct dlList *list);
00116 /* Get the node before the head of the list */
00117 
00118 struct dlNode *dlGetAfterTail(struct dlList *list);
00119 /* Get the node after the tail of the list */
00120 
00121 void dlSort(struct dlList *list, int (*compare )(const void *elem1,  const void *elem2));
00122 /* Sort a doubly linked list with Qsort and a temporary array. 
00123  * The arguments to the compare function in real, non-void, life
00124  * are pointers to pointers of the type that is in the val field of 
00125  * the nodes of the list. */
00126 
00127 void *dlListToSlList(struct dlList *dList);
00128 /* Return slList from dlList. */
00129 
00130 void dlCat(struct dlList *a, struct dlList *b);
00131 /* Move items from b to end of a. */
00132 
00133 struct dlNode *dlValInList(struct dlList *list, void *val);
00134 /* Return node on list if any that has associated val. */
00135 
00136 #endif /* DLIST_H */
00137 
00138 

Generated on Tue Dec 25 18:39:29 2007 for blat by  doxygen 1.5.2