00001 /*! \file list.h 00002 Header file for list.c 00003 */ 00004 00005 #ifdef __cplusplus 00006 extern "C" { 00007 #endif 00008 00009 00010 #ifndef LIST_H 00011 #define LIST_H 00012 00013 #include <stdlib.h> 00014 00015 /* ----------------------------------------------------------------------- */ 00016 /* Data structures */ 00017 /* ----------------------------------------------------------------------- */ 00018 00019 /*! \brief This structure defines one element of the linked list. 00020 */ 00021 00022 typedef struct ListElmt_ 00023 { 00024 /*! \brief Pointer to the element's data. */ 00025 void *donnee; 00026 00027 /*! \brief Pointer to the next element in the linked list. */ 00028 struct ListElmt_ *suivant; 00029 00030 #ifdef DOUBLE_LINK 00031 00032 /*! \brief Pointer to the previous element in the linked list. */ 00033 struct ListElmt_ *precedent; 00034 00035 #endif 00036 00037 } ListElmt; 00038 00039 /*! \brief This structure defines a linked list. 00040 */ 00041 00042 typedef struct List_ 00043 { 00044 /*! \brief Number of elements in the linked list. */ 00045 int taille; 00046 00047 /*! \brief Pointer to the user defined function used to compare two elements. */ 00048 int (*corresp)(const void *val1, const void *val2); 00049 00050 /*! \brief Pointer on to the user defined function used to free the element's data memory. */ 00051 void (*detruire)(void *donnee); 00052 00053 /*! \brief Pointer to the beginning of the linked list. */ 00054 ListElmt *tete; 00055 00056 /*! \brief Pointer to the end of the linked list. */ 00057 ListElmt *queue; 00058 } List; 00059 00060 /* ----------------------------------------------------------------------- */ 00061 /* library's API */ 00062 /* ----------------------------------------------------------------------- */ 00063 00064 void list_init(List *liste, void (*detruire)(void *donnee), int (*corresp)(const void *val1, const void *val2)); 00065 00066 void list_destroy(List *liste); 00067 00068 int list_ins_next(List *liste, ListElmt *element, const void *donnee); 00069 00070 int list_rem_next(List *liste, ListElmt *element, void **donnee); 00071 00072 ListElmt* list_find (List *liste, ListElmt* from, void* data, ListElmt **prec); 00073 00074 /* ----------------------------------------------------------------------- */ 00075 /* Library's macros */ 00076 /* ----------------------------------------------------------------------- */ 00077 00078 /*! \brief Return the number of elements in a linked list. 00079 \param liste Pointer to the list structure. 00080 */ 00081 00082 #define list_size(liste) ((liste)->taille) 00083 00084 /*! \brief Return a pointer to the first element of the linked list. 00085 \param liste Pointer to the list structure. 00086 */ 00087 00088 #define list_head(liste) ((liste)->tete) 00089 00090 /*! \brief Return a pointer to the last element of the linked list. 00091 \param liste Pointer to the list structure. 00092 */ 00093 00094 #define list_tail(liste) ((liste)->queue) 00095 00096 /*! \brief Test if a given element is the first element of a linked list. 00097 \param liste Pointer to the list structure. 00098 \param element Pointer to the linked list element. 00099 \return This macro returns 1 if <i>element</i> point to the beginning of <i>liste</i>. Otherwise it returns 0. 00100 */ 00101 00102 #define list_is_head(liste, element)((element) == (liste)->tete ? 1 : 0) 00103 00104 /*! \brief Test if a given element is the last element of a linked list. 00105 \param liste Pointer to the list structure. 00106 \param element Pointer to the linked list element. 00107 \return This macro returns 1 if <i>element</i> point to the end of <i>liste</i>. Otherwise it returns 0. 00108 */ 00109 00110 #define list_is_tail(element)((element)->suivant == NULL ? 1 : 0) 00111 00112 /*! \brief Return a pointer to the data hold by an element. 00113 \param element Pointer to the linked list element. 00114 \return This macro returns a pointer to the data associted with <i>element</i>. 00115 */ 00116 00117 #define list_data(element) ((element)->donnee) 00118 00119 /*! \brief Return a pointer to the next element in the linked list. 00120 \param element Pointer to the linked list element. 00121 \return This macro returns a pointer to the element next to <i>element</i>. 00122 */ 00123 00124 #define list_next(element) ((element)->suivant) 00125 00126 #ifdef DOUBLE_LINK 00127 00128 /*! \brief Return a pointer to the previous element in the linked list. 00129 \param element Pointer to the linked list element. 00130 \return This macro returns a pointer to the element previous to <i>element</i>. 00131 */ 00132 00133 #define list_prev(element) ((element)->precedent) 00134 #endif 00135 00136 #endif 00137 00138 00139 00140 #ifdef __cplusplus 00141 } 00142 #endif 00143