00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef __CLIST_H__
00038 #define __CLIST_H__
00039
00040 #if defined __cplusplus
00041 #define CLINK extern "C"
00042 #else
00043 #define CLINK
00044 #endif
00045
00053 #ifdef CLIST_IMPLEMENT_HERE
00054
00055 #if defined __cplusplus
00056 #include <cstdlib>
00057 #else
00058 #include <stdlib.h>
00059 #endif
00060
00061
00062
00063 #define DECLARE_LIST(x) typedef struct t_##x##List { \
00064 x* data; \
00065 struct t_##x##List * next; \
00066 } *x##List; \
00067 \
00068 CLINK x##List addTo##x##List (x##List l, x *data) \
00069 { \
00070 x##List cell = (x##List) malloc(sizeof(struct t_##x##List)); \
00071 cell->next = l; \
00072 cell->data = data; \
00073 return cell; \
00074 } \
00075 \
00076 CLINK x##List appendTo##x##List (x##List l, x *data) \
00077 { \
00078 if (!l) { \
00079 x##List res = (x##List) malloc(sizeof(struct t_##x##List)); \
00080 res->next = NULL; \
00081 res->data = data; \
00082 return res; \
00083 } else { \
00084 l->next = appendTo##x##List (l->next, data); \
00085 return l; \
00086 } \
00087 } \
00088 \
00089 CLINK void delete##x##List (x##List l, int deleteData) \
00090 { \
00091 if (!l) return; \
00092 delete##x##List (l->next, deleteData); \
00093 if (deleteData) \
00094 delete##x(l->data); \
00095 free(l); \
00096 } \
00097 \
00098 CLINK x##List join##x##Lists (x##List pre, x##List post) \
00099 { \
00100 x##List i; \
00101 \
00102 if (post) { \
00103 if (!pre) return post; \
00104 for (i = pre; i->next != NULL; i = i->next); \
00105 i->next = post; \
00106 } \
00107 return pre; \
00108 }
00109
00110 #else
00111
00112
00113
00114 #define DECLARE_LIST(x) typedef struct t_##x##List { \
00115 x* data; \
00116 struct t_##x##List * next; \
00117 } *x##List; \
00118 \
00119 CLINK x##List addTo##x##List (x##List l, x *data); \
00120 \
00121 CLINK x##List appendTo##x##List (x##List l, x *data); \
00122 \
00123 CLINK void delete##x##List (x##List l, int deleteData); \
00124 \
00125 CLINK x##List join##x##Lists (x##List pre, x##List post); \
00126
00127 #endif
00128
00129 #endif