1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| #include <stdio.h>
#include <stdlib.h>
/* Déclaration des structures */
typedef struct
{
int x;
int y;
} coordinates;
struct node
{
coordinates* coor;
struct node* next;
};
typedef struct
{
int length;
struct node* first;
} list;
/* Constructeur et destructeur d'un objet de type coodinates + fonction de comparaison*/
coordinates * coordinates_new(int x, int y)
{
coordinates *tmp = malloc(sizeof *tmp);
if (tmp == NULL)
{
fprintf(stderr, "L'allocation a échoué dans coordinates_new()!\n");
exit(EXIT_FAILURE);
}
tmp->x = x;
tmp->y = y;
return tmp;
}
int coordinates_compare(coordinates *c1, coordinates *c2)
{
return (c1->x == c2->x) && (c1->y == c2->y);
}
void coordinates_free(coordinates **c)
{
if (*c != NULL)
{
free(*c);
*c = NULL;
}
}
/* Interface pour une gestion minimaliste de ta liste chaînée.
Très incomplet!*/
list * list_new(void)
{
list *tmp = malloc(sizeof *tmp);
if (tmp == NULL) /* L'allocation a échoué*/
{
fprintf(stderr, "L'Allocation a échoué dans list_new()!\n");
exit(EXIT_FAILURE);
}
tmp->length = 0;
tmp->first = NULL;
return tmp;
}
void list_free(list **pp_l)
{
struct node *current;
struct node *tmp;
if ((*pp_l != NULL) && (tmp = (*pp_l)->first))
{
for (current = tmp->next; current != NULL; tmp = current, current = current->next)
{
coordinates_free(&(tmp->coor));
free(tmp);
}
coordinates_free(&(tmp->coor));
free(tmp);
free(*pp_l), *pp_l = NULL;
}
}
int list_add(list *lst, coordinates *c)
{
int result;
struct node *tmp = malloc(sizeof *tmp);
if (tmp != NULL)
{
tmp->coor = c;
tmp->next = lst->first;
lst->first = tmp;
(lst->length)++;
result = EXIT_SUCCESS;
}
else
{
fprintf(stderr, "Allocation failed in add()!\n");
result = EXIT_FAILURE;
}
return result;
}
int list_search(list* l, coordinates* c)
{
int i;
struct node* currentNode = NULL;
for (i = 1, currentNode = l->first; currentNode != NULL; currentNode = currentNode->next)
{
if (coordinates_compare(c, currentNode->coor))
{
return i;
}
++i;
}
return -1;
}
/* Main entry point! */
int main(void)
{
int index;
list *lst = NULL;
coordinates *c1;
coordinates *c2;
coordinates *c3;
/* On crée quelques objets coordonnées*/
c1 = coordinates_new(1, 4);
c2 = coordinates_new(10, 40);
c3 = coordinates_new(100, 400);
/* On crée une nouvelle liste*/
lst = list_new();
/* On ajoute des éléments à la liste */
list_add(lst, c1);
list_add(lst, c2);
list_add(lst, c3);
/* On recherche la position d'un objet dans la liste */
index = list_search(lst, c3);
if (index != -1)
{
printf("La position de l'objet cherché est %d\n", index);
}
else
{
printf("L'élément recherché n'a pas été trouvé!\n");
}
/* Bien sûr, ne pas oublier de détruire la liste*/
list_free(&lst);
return EXIT_SUCCESS;
} |
Partager