1 | #ifndef LISTH |
---|
2 | #define LISTH |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <string.h> |
---|
6 | /* |
---|
7 | une fonction d erreur -> EXIT |
---|
8 | */ |
---|
9 | #define ErrExit(mesg) {fprintf(stderr, "Fatal: %s\n", (char *)(mesg)); exit(1);} |
---|
10 | |
---|
11 | typedef void * NODE; |
---|
12 | |
---|
13 | typedef int (*PFI)(); /* pointeur vers fonction retournant int */ |
---|
14 | |
---|
15 | /** |
---|
16 | * gestion de listes chainées génériques simplifiees |
---|
17 | * definition de la structure d'un élément d'une liste chainée |
---|
18 | */ |
---|
19 | typedef struct _Elt |
---|
20 | { |
---|
21 | NODE value; /**< le pointeur a stoquer */ |
---|
22 | struct _Elt * next; /**< l'élément suivant */ |
---|
23 | struct _Elt * previous; /**< l'élément précedent */ |
---|
24 | }Elt, *ptrElt; |
---|
25 | |
---|
26 | /** |
---|
27 | * définition de da structure de liste générique |
---|
28 | */ |
---|
29 | typedef struct { |
---|
30 | int count; /**< le nombre d'elements de la liste */ |
---|
31 | ptrElt first; /**< premier élément de la liste */ |
---|
32 | ptrElt last; /**< dernier élément de la liste */ |
---|
33 | ptrElt current; /**< courant élément (interne pour tri) */ |
---|
34 | } _List, *LIST; |
---|
35 | |
---|
36 | /** Constructor, creates an empty list |
---|
37 | * @return a new LIST |
---|
38 | */ |
---|
39 | LIST make_list(); |
---|
40 | |
---|
41 | /** Destroyer, free the list |
---|
42 | * @param list the list to free |
---|
43 | */ |
---|
44 | void free_list(LIST list); |
---|
45 | |
---|
46 | /** Sorts the list with a given function |
---|
47 | * @param list the list to sort |
---|
48 | * @param func a PFI function |
---|
49 | * @return the list sorted |
---|
50 | */ |
---|
51 | LIST sort_list_func(LIST list, PFI func); |
---|
52 | |
---|
53 | NODE search_list(LIST list, NODE node, PFI func); |
---|
54 | NODE search_list_ptr(LIST list, NODE ident, NODE var, NODE code, PFI func); |
---|
55 | |
---|
56 | |
---|
57 | /** Adds an element at the end of the list |
---|
58 | * @param list the list to add an element to |
---|
59 | * @param value the element to include |
---|
60 | * @return NODE the included element |
---|
61 | */ |
---|
62 | NODE add_to_tail(LIST list, NODE value); |
---|
63 | |
---|
64 | /** Deletes a node from th elist |
---|
65 | * @param list the list to delete an element from |
---|
66 | * @param node the node to destroy |
---|
67 | * @return NODE the destroyed node |
---|
68 | */ |
---|
69 | NODE delete_list_node(LIST list, NODE node); |
---|
70 | |
---|
71 | |
---|
72 | /** Use this to iterate in a list. |
---|
73 | * You have to use it like a "for" |
---|
74 | * i.e. : loop_through_list(list, ptr , type){...} |
---|
75 | * |
---|
76 | * @param list the LIST to iterate |
---|
77 | * @param ptr the pointer that will contain the extracted elements |
---|
78 | * @param type The type of the elements to extract |
---|
79 | */ |
---|
80 | #define loop_through_list( list, ptr , type) \ |
---|
81 | for ( (list)->current = (list)->first ,\ |
---|
82 | ptr = ((list)->current? (type)(list)->current->value: (type)NULL); \ |
---|
83 | (list)->current != NULL ; \ |
---|
84 | (list)->current= (list)->current->next, \ |
---|
85 | ptr = ((list)->current? (type)(list)->current->value: (type)NULL)) |
---|
86 | |
---|
87 | /** Retreive the last element from a list |
---|
88 | * @param list the LIST |
---|
89 | * @return NODE the last element |
---|
90 | */ |
---|
91 | #define get_list_tail( list ) \ |
---|
92 | ( (list) ? ((list)->last ? (list)->last->value: NULL) : NULL ) |
---|
93 | |
---|
94 | /** Retreive the first element from a list |
---|
95 | * @param list the LIST |
---|
96 | * @return NODE the first element |
---|
97 | */ |
---|
98 | #define get_list_head( list ) \ |
---|
99 | ( list ? (list->first ? list->first->value: NULL) : NULL ) |
---|
100 | |
---|
101 | /** Reurns the length of the list (the number of elements) |
---|
102 | * @param list the LIST |
---|
103 | * @return int the size of the list |
---|
104 | */ |
---|
105 | #define list_length( list ) (list)->count \ |
---|
106 | |
---|
107 | #endif |
---|
108 | |
---|