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
| #include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#define MAX 30
typedef struct Employe
{
char nom[MAX];
int salaire;
struct Employe* suivant;
}Employe;
typedef Employe* Liste;
void ajouteEmploye(Liste *L,Employe *e)
{
//ajoute un employe en tete de liste
Liste tmp = malloc(sizeof(Employe));
tmp->nom = (char*)malloc((strlen(e->nom)+1)*sizeof(char));
strcpy(tmp->nom,e->nom);
tmp->salaire = e->salaire;
tmp->suivant = *L;
*L = tmp;
}
void afficheEmploye(Employe* e)
{
if (e)
printf("%s\t%d\n",e->nom,e->salaire);
}
void afficheListe(Liste L)
{
while (L)
{
afficheEmploye(L);
L=L->suivant;
}
}
int main()
{
char * nom;
int salaire;
Employe * e = malloc(sizeof(Employe));
printf("entrez le nom d'un employe et son salaire\n");
scanf("%s",nom);
scanf("%d",&salaire);
e->nom = malloc((strlen(nom)+1)*sizeof(char));
strcpy(e->nom,nom);
e->salaire = salaire;
Liste L = NULL;
ajouteEmploye(&L,e);
afficheListe(L);
return 0;
} |
Partager