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
|
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define LG 10
struct element{ /*Déclaration de la structure*/
char nom[LG+1];
int age;
struct element *suivant; /*Pointeur sur l'élément suivant*/
};
void creation (struct element * *); /*Prototype des fonctions*/
void affiche (struct element *);
int main (void)
{
struct element *fiche;
creation(&fiche);
affiche(fiche);
getch ();
return (0);
}
/*********Fonction de création de liste chainée**********/
void creation (struct element * *adr)
{
char nom[LG+1];
int age;
struct element *courant;
printf ("Entrer un nom vide pour terminer\n\n");
*adr=NULL;
while (1)
{
printf ("Nom :");
gets(nom);
if (!strlen(nom)) break;
printf ("Age :");
scanf ("%d",&age);
getchar();
courant=(struct element *)malloc (sizeof(struct element)); /*réservation mémoire*/
strcpy(courant->nom,nom);
courant->age=age;
courant->suivant=*adr;
*adr=courant;
}
}
/*******Fonction d'affichage de la liste chainée******/
void affiche (struct element *point)
{
printf ("\n\tNOM\tAGE\n\n");
while (point)
{
printf ("%12s",point->nom);
printf ("%7d\n",point->age);
point=point->suivant;
}
} |
Partager