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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_noeud {
char *nom;
struct s_noeud *next;
} t_node;
typedef struct {
t_node *racine;
} t_liste;
t_node *newNode(char *nom)
{
t_node *node;
node=malloc(sizeof(t_node));
node->nom=strdup(nom);
node->next=NULL;
return node;
}
void append(t_liste *liste, t_node *node)
{
t_node *current;
if (liste->racine == NULL)
{
liste->racine=node;
return;
}
for (current=liste->racine; current->next != NULL; current=current->next);
current->next=node;
}
void init(t_liste *liste)
{
liste->racine=NULL;
}
void affiche(t_liste *liste)
{
t_node *current;
unsigned short i;
if (liste->racine == NULL)
{
printf("Liste vide !!!\n");
return;
}
for (current=liste->racine, i=1; current != NULL; current=current->next, i++)
printf("Noeud [%d]: %s\n", i, current->nom);
}
int main()
{
t_liste liste;
init(&liste);
affiche(&liste);
append(&liste, newNode("toto"));
append(&liste, newNode("titi"));
append(&liste, newNode("tata"));
affiche(&liste);
} |