Liste doublement chainée en C
J'aurais besoin d'un coup de main à déboguer cette liste doublement chainée, en particulier les fonctions append(), appbeg() voici le code:
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 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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct dlist {
int i;
struct dlist *prev;
struct dlist *next;
};
struct dlist *init(int j) {
struct dlist *p = (struct dlist *) malloc(sizeof(struct dlist));
p->i = j;
p->prev = p->next = NULL;
return p;
}
void print(struct dlist *p) {
printf("%d %p %p\n", p->i, p->prev, p->next);
}
struct dlist *append(struct dlist *new) {
struct dlist *first = init(0), *head = init(0);
if(new->next == NULL) {
first->i = new->i;
first->prev = new->prev;
first->next = new->next; // jusqu'ici ca va
first->prev->prev = NULL; // ici et ensuite je ne maîtrise plus
first->next->next = new;
return first;
}
if(new->next != NULL) { /* une address */
struct dlist *head = init(0);
/* Avance jusqu'au dernier noeud... volontairement enlevé, sinon à partir du dernier noeud, pointe le nouveau */
new->i = head->i; // jusqu'ici ca va
new->prev = head->prev;
new->next = head->next;
/* Puis la je ne maitrise plus */
return new;
}
return new;
}
struct dlist *appbeg(struct dlist *old) { /* celle la plante aussi */
struct dlist *new = init(0);
new->i = old->i;
new->prev = old->prev;
new->next = old->next;
new->prev->next= old;
new->next->prev = old;
return new;
}
int main(int argc, char **argv) {
int i = 0;
while(i++ < 10000) print(append(init(0)));
return 0;
} |
J'ai déjà trouvé plusieurs exemples de code sur l'Internet, mais je préférerais travailler sur ce bout de code.