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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #include <conio.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct tnoeud
{
int ele ;
struct tnoeud *suiv ;
} Tnoeud ;
typedef struct
{
Tnoeud *debut ;
Tnoeud *fin ;
int lg ;
}liste ;
/* -fy- variable globale, c'est mal*/
/*creer un noeud */
Tnoeud * creer_noeud (int x)
{
Tnoeud *p ;
/* -fy- On ne cast plus malloc */
p= malloc (sizeof(Tnoeud));
if (p)
{
p->ele = x ;
p->suiv = NULL ;
}
return p ;
}
/* -fy- pas un bon prototype pour une fonction initialiser*/
liste *initialiser ()
{
liste *adl = malloc(sizeof *adl);
if(adl)
{
adl->debut=NULL ;
adl->fin=NULL;
adl->lg = 0 ;
}
return adl;
}
void inserer ( liste *adl , int x )
{
Tnoeud *p = creer_noeud(x);
/*-fy- Je pense que c'est la liste que tu veux tester
Si elle est vide alors p est le debut et la fin
Mais d'abord on teste la valeur de p
*/
if(p!=NULL)
{
if (adl->debut==NULL)
{
adl->debut = adl->fin = p ;
adl->lg+=1;
}
else
{
adl->fin->suiv = p;
adl->fin = p ;
adl->lg+=1;
}
}
printf("la longeur est : %d \n",adl->lg);
}
void affichage(liste *adl)
{
Tnoeud *p ;
p=adl->debut ;
/* -fy- Plutôt un while, p peut être tout de suite à NULL */
while(p)
{
/*-fy- C'est peut-être p->x, non? */
printf("%d",p->ele);
p=p->suiv ;
}
}
/* -fy- C'est int main */
int main (void)
{
char tmp[128];
long x;
liste *adl;
/* apel fonction inserer*/
/* -fy- Ta fonction initialiser ne pouvait pas marcher comme cela */
adl = initialiser();
do
{
printf("entrer une valeur : ");
/* -fy- On n'utilise plus scanf mais fgets*/
fgets(tmp,sizeof tmp, stdin);
x = strtol(tmp,NULL,0);
inserer(adl,x);
printf("voulez vous inserer: ");
/*-fy- On n'utilise pas scanf et en plus celui là était faux...*/
fgets(tmp,sizeof tmp,stdin);
}
while(!strcmp(tmp,"o\n"));
/*affichage*/
affichage(adl);
getch();
return 0;
} |
Partager