Se connecter avec
S'enregistrer | Connectez-vous

probleme d affichage une liste chainee

Dernière réponse : dans Programmation

salut

j ai un probleme lors d affichage d une liste chaine. le probleme lors d inserer les valeur par exemple j insere 5 et 7 et 8 dans l affichage je trouve 8 8 8
la valeur 8 se repete 3 fois :

voici mon script d affichage liste chainée :

void affichage(liste *adl)
{
Tnoeud *p ;
p=adl->debut ;
do
{
printf("%d",x);
p=p->suiv ;
}
while((p->suiv)!=NULL);
}

s il y a quelqu un peut corriger la procedure affichage .

merci d avance
Lassé par la pub ? Créez un compte

voici mon programme :
#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 ;

liste *adl ;
int n,i,x,lg;
char rep ;
/*creer un noeud */

Tnoeud * creer_noeud (int x)
{
Tnoeud *p ;

p= (Tnoeud * )malloc (sizeof(Tnoeud));

if (p)
{
p->ele = x ;
p->suiv = NULL ;
}
return p ;

}

void initialiser (liste *adl)
{

adl->debut=NULL ;
adl->fin=NULL;
adl->lg = 0 ;
}

void inserer ( liste *adl , int x )
{
Tnoeud *p = creer_noeud(x);

if (p)
{
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 ;
do
{
printf("%d",x);
p=p->suiv ;
}
while((p->suiv)!=NULL);
}

void main ()
{
clrscr();

/* apel fonction inserer*/
initialiser(adl);
do{
printf("entrer une valeur : ");
scanf("%d",&x);
inserer(adl,x);
printf("voulez vous inserer: ");
scanf("%s",&rep);
}
while(rep=='o');
/*affichage*/

affichage(adl);

getch();

}



  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef struct tnoeud {
  5. int ele ;
  6.  
  7. struct tnoeud *suiv ;
  8.  
  9. } Tnoeud ;
  10.  
  11. typedef struct {
  12.  
  13. Tnoeud *debut ;
  14. Tnoeud *fin ;
  15. int lg ;
  16.  
  17. }liste ;
  18.  
  19. liste *adl ;
  20. int n,i,x,lg;
  21. char rep ;
  22. /*creer un noeud */
  23.  
  24. Tnoeud * creer_noeud (int x)
  25. {
  26. Tnoeud *p ;
  27.  
  28. p= (Tnoeud * )malloc (sizeof(Tnoeud));
  29.  
  30. if (p)
  31. {
  32. p->ele = x ;
  33. p->suiv = NULL ;
  34. }
  35. return p ;
  36.  
  37. }
  38.  
  39. void initialiser (liste *adl)
  40. {
  41.  
  42. adl->debut=NULL ;
  43. adl->fin=NULL;
  44. adl->lg = 0 ;
  45. }
  46.  
  47. void inserer ( liste *adl , int x )
  48. {
  49. Tnoeud *p = creer_noeud(x);
  50.  
  51. if (p)
  52. {
  53. adl->debut = adl->fin = p ;
  54. adl->lg+=1;}
  55. else
  56. {
  57. adl->fin->suiv = p;
  58. adl->fin = p ;
  59. adl->lg+=1;
  60. }
  61.  
  62. printf("la longeur est : %d \n",adl->lg);
  63. }
  64. void affichage(liste *adl)
  65. {
  66. Tnoeud *p ;
  67. p=adl->debut ;
  68.  
  69. do{
  70. printf("%d",x);
  71. p=p->suiv ; }
  72. while((p)!=NULL);
  73. }
  74.  
  75. void main ()
  76. {
  77. clrscr();
  78.  
  79. /* apel fonction inserer*/
  80. initialiser(adl);
  81. do{
  82. printf("entrer une valeur : ");
  83. scanf("%d",&x);
  84. inserer(adl,x);
  85. printf("voulez vous inserer: ");
  86. scanf("%s",&rep);
  87. }
  88. while(rep=='o');
  89. /*affichage*/
  90.  
  91. affichage(adl);
  92.  
  93. getch();
  94.  
  95. }

nacerleroi11 a dit :
  1. liste *adl ;
  2. <...>
  3. void main ()
  4. {
  5. /* apel fonction inserer*/
  6. initialiser(adl);
  7. <...>

  • main() retourne int. Toujours.
  • adl est un pointeur invalide (non initialisé ou NULL). Passer une telle valeur à une fonction invoque un comportement indéfini.
    Tu veux probablement (pas besoin de globale...)
    1. <...>
    2. int main (void)
    3. {
    4. liste adl ;
    5.  
    6. initialiser(&adl);
    7. <...>

    Citation :

    1. char rep ;
    2.  
    3. void main ()
    4. {
    5. scanf("%s",&rep);

    Horrible !
    "%s" attend l'adresse du premier élément d'un tableau. Pour "o", il faut un tableau d'au moins 2 caractères : {'o', 0}
    1. int main (void)
    2. {
    3. char rep[8] ;
    4.  
    5. scanf("%7s",rep);

    Lassé par la pub ? Créez un compte