rechercher de point dans structure
Bonjours,
voila en fait je chercher a compare les point dont les coordonnees sont dans une liste pour savoir si ce point si trouve ( ici dans la listeFermee) , mais le probleme c'est kelle ne me compare jamais le dernier point.
Code:
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
|
int RechercheListeFermee(int m, int n) {
struct point *o;
o = (struct point *) malloc(sizeof(struct point));
(*o).indiceligne = m;
(*o).indicecolonne = n;
(*o).F = 0.0;
(*o).G = 0.0;
(*o).H = 0.0;
(*o).suivant = NULL;
if (ListeFermee != NULL){
struct point *pcourant = ListeFermee;
while ((*pcourant).suivant != NULL) {
if (((*pcourant).indiceligne == m ) && ((*pcourant).indicecolonne == n)){
return 0;
}
else{
pcourant = (*pcourant).suivant; }
}
return 1 ;
}
} |
J'espere être assez clair.
Merci de votre aide.
Re: rechercher de point dans structure
Citation:
Envoyé par cool17
Code:
1 2 3 4 5
|
struct point *o;
o = (struct point *) malloc(sizeof(struct point));
(*o).indiceligne = m;
(*o).indicecolonne = n; |
Cast inutile. Codage inutilement lourd...
http://emmanuel-delahaye.developpez....tes.htm#malloc
Je conseille l'écriture ->, c'est fait pour...
Code:
1 2 3
| struct point *o= malloc(sizeof *o);
o->indiceligne = m;
o->indicecolonne = n; |
etc.
Mais comme il a été dit, l'adresse de cette structure n'est pas mémorisée, et le bloc n'est pas non plus libéré. Ou alors il manque du code...
Et si elle est locale, pourquoi avoir fait une allocation dynamique ?
Re: rechercher de point dans structure
Citation:
Envoyé par cool17
Code:
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
|
int RechercheListeFermee(int m, int n) {
struct point *o;
o = (struct point *) malloc(sizeof(struct point));
(*o).indiceligne = m;
(*o).indicecolonne = n;
(*o).F = 0.0;
(*o).G = 0.0;
(*o).H = 0.0;
(*o).suivant = NULL;
if (ListeFermee != NULL){
struct point *pcourant = ListeFermee;
while ((*pcourant).suivant != NULL) {
if (((*pcourant).indiceligne == m ) && ((*pcourant).indicecolonne == n)){
return 0;
}
else{
pcourant = (*pcourant).suivant; }
}
return 1 ;
}
} |
Moi j'ecrirai en supposant que ListeFerme est une variable globale (car je vois pas d'où elle vienne) et qu'elle de type struct point *:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int RechercheListeFermee(int m, int n) {
struct point *pcourant ; /* les declaration se fassent avant les traitements en C */
if (ListeFermee != NULL)
{
pcourant = ListeFermee;
while (pcourant != NULL)
{
if ((pcourant->indiceligne == m ) && (pcourant->indicecolonne == n))
{
return 0;
}
else
{
pcourant = pcourant->suivant;
}
}
return 1 ;
}
} |