[code] liste doublement chainée
Le
Nicolas Aunai
salut, j'ai un petit problème a comprendre une fonction s'applicant a
une liste chainée, voici les structures de la liste :
/* atom structure: */
typedef struct _atom_t {
struct _atom_t *next;
struct _atom_t *prev;
void *data;
} atom_t, *patom_t;
/* list structure: */
typedef struct {
int count;
patom_t first;
patom_t curr;
patom_t last;
} list_t, *plist_t;
bon je sais pas beau cacher pointeurs :-) mais bon c'est pas mon
code bref, voici la fonciton qui me pose problème :
void *
_list_del_(plist_t pl) {
void * data; /*pour recuperer les donnees ok*/
patom_t pa = NULL; /*le maillon a virer*/
/* sanity check: */
if (!pl || !pl->curr) return NULL;
pl->count--;/*on enleve 1 au nombre de maillons*/
pa = pl->curr;/*récupère le maillon courant*/
data = pa->data;/*récupère les données du maillon*/
/*la je comprend plus.. on recule et avance, pourquoi ?*/
if (pa->prev) pa->prev->next = pa->next;
else pl->first = pa->next;
/*idem, on avance et recule quel intéret*/
if (pa->next) pa->next->prev = pa->prev;
else pl->last = pa->prev;
if (!pa->next && pa->prev) pl->curr = pa->prev;
else pl->curr = pa->next;
mem_clean(pa);/*une ptite fonction qui en gros équivaut a un free()*/
return data;
}
merci !
pour ceux que ça intéresse, le code provient de la page :
http://ilay.org/yann/articles/genericite/
a+
--
Nico,
http://astrosurf.com/nicoastro
messenger : nicolas_aunai@hotmail.com
une liste chainée, voici les structures de la liste :
/* atom structure: */
typedef struct _atom_t {
struct _atom_t *next;
struct _atom_t *prev;
void *data;
} atom_t, *patom_t;
/* list structure: */
typedef struct {
int count;
patom_t first;
patom_t curr;
patom_t last;
} list_t, *plist_t;
bon je sais pas beau cacher pointeurs :-) mais bon c'est pas mon
code bref, voici la fonciton qui me pose problème :
void *
_list_del_(plist_t pl) {
void * data; /*pour recuperer les donnees ok*/
patom_t pa = NULL; /*le maillon a virer*/
/* sanity check: */
if (!pl || !pl->curr) return NULL;
pl->count--;/*on enleve 1 au nombre de maillons*/
pa = pl->curr;/*récupère le maillon courant*/
data = pa->data;/*récupère les données du maillon*/
/*la je comprend plus.. on recule et avance, pourquoi ?*/
if (pa->prev) pa->prev->next = pa->next;
else pl->first = pa->next;
/*idem, on avance et recule quel intéret*/
if (pa->next) pa->next->prev = pa->prev;
else pl->last = pa->prev;
if (!pa->next && pa->prev) pl->curr = pa->prev;
else pl->curr = pa->next;
mem_clean(pa);/*une ptite fonction qui en gros équivaut a un free()*/
return data;
}
merci !
pour ceux que ça intéresse, le code provient de la page :
http://ilay.org/yann/articles/genericite/
a+
--
Nico,
http://astrosurf.com/nicoastro
messenger : nicolas_aunai@hotmail.com

Poser une question


ok avec un dessin on s'en sort, pl->prev->next, on recule et avance
mais on arrive pas au même endroit d'où on est venu, ce qui revient a
"couper" le lien entre deux maillons pour insérer un autre...
--
Nico,
http://astrosurf.com/nicoastro
messenger :