Problème de liste chainée
Bonjour,
J'essai de comprendre le fonctionnement des listes chainées (simple pour commencer). Malgrès les tutoriels que j'ai pu trouvé sur ce site, je n'arrive toujours pas à faire une liste chainée qui fonctionne..:cry:
En fait apparement la liste est bien remplie, mais je n'arrive pas à la relire.
Pourriez vous me dire quelle est mon erreur ?
Merci
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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
/*------------------ liste chainee ----------------------------------*/
typedef struct Event sEvent;
struct Event {
char value[10];
struct Event *pnext;
};
typedef sEvent *ListEvent;
/*---------------------- fonction ------------------------------------*/
/* Ajout d'un event dans la liste */
ListEvent AddEvent(ListEvent List, char *val) {
ListEvent NewEvent = malloc(sizeof(sEvent));
if (NewEvent != NULL) { /* alloc memoire OK */
strcpy(NewEvent->value,val);
printf("Event:%s\n",NewEvent->value);
NewEvent->pnext = List;
return List;
} else {
return NULL; /* pb memoire retourne NULL */
}
}
/* lecture de la liste */
void ViewEvent(ListEvent List) {
sEvent *tmp=List;
while (tmp != NULL) {
printf("val:%s\n",tmp->value);
tmp = tmp->pnext;
}
}
/*-------------------------- MAIN ------------------------------------*/
int main( int argc,char *argv[]) {
ListEvent List = NULL;
char *pch=NULL;
int a=0;
char data[]="DATA1:DATA2:DATA3:DATA4";
char msgerror[50];
memset(msgerror,'\0',50);
pch = strtok(data,":");
while (pch != NULL) {
List = AddEvent(List,pch);
pch = strtok(NULL,":");
}
ViewEvent(List);
return 0;
} |