Erreur de segmentation
monix927
Messages postés
9
Statut
Membre
-
lami20j Messages postés 21506 Date d'inscription Statut Modérateur, Contributeur sécurité Dernière intervention -
lami20j Messages postés 21506 Date d'inscription Statut Modérateur, Contributeur sécurité Dernière intervention -
Bonjour,
j'ai cree un arbre en c mais j ai toujours cette erreur :"erreur de segmentation"et j'aimeria que quelqu'un me vienne en aide voici le code:
#include<stdio.h>
#include<stdlib.h>
typedef struct noeud *arbre;
typedef struct noeud
{int val;
arbre g;
arbre d;
}noeud;
typedef struct pile
{
arbre t[100];
int top;
char sens;
}pile;
int pile_pleine(pile p)
{
p.top=100;
return 0;
}
int pile_vide(pile p)
{
p.top=0;
return 0;
}
void empiler(arbre x,pile p)
{ if (!pile_pleine(p))
{
p.top=p.top+1;
p.t[p.top]=x;
}
else
{
printf("pile déja pleine");
}
}
void depiler(arbre x,pile p)
{if (pile_vide==0)
{ printf("pile deja vide");}
else
{p.top=p.top-1;}
}
void vider_pile(pile p)
{p.top=0;
}
void infixe(arbre a)
{pile s;
arbre p;
vider_pile(s);
p=a;
while ((p!=NULL)||(!pile_vide(s)==0))
{while (p!=NULL)
{empiler(a,s);
p=p->g;
}
depiler(a,s);
printf("%d\t",p->val);
p=p->d;
}
}
void prefixe(arbre a)
{pile s;
arbre p;
vider_pile(s);
p=a;
while ((p!=NULL)||(!pile_vide(s)==0))
{while (p!=NULL)
{empiler(a,s);
printf("%d\t",p->val);
p=p->g;
}
depiler(a,s);
p=p->d;
}
}
void postfixe(arbre a)
{pile s;
arbre p;
vider_pile(s);
p=a;
while ((p!=NULL)||(!pile_vide(s)==0))
{while (p!=NULL)
{empiler(a,s);
p=p->g;
}
depiler(a,s);
printf("%d\t",p->val);
p=p->d;
}
}
void ajout(int x,arbre a)
{if (a=NULL)
{
a=(arbre)malloc(1*sizeof(arbre));
a->val=x;
a->g=NULL;
a->d=NULL;
}
else
{
if(x<a->val)
{ajout(x,a->g);}
else
{ajout(x,a->d);}
}
}
int main()
{int x;
char rep;
arbre t;
do{
printf("ajouter un nombre\n");
scanf("%d",&x);
ajout(x,t);
printf("voulez-vous saisir a nouveau (o/n)\n");
}while(rep='o');
printf("affichage ave le parcours infixe\n");
infixe(t);
printf("affichage ave le parcours prefixe\n");
prefixe(t);
return 0;
}
j'ai cree un arbre en c mais j ai toujours cette erreur :"erreur de segmentation"et j'aimeria que quelqu'un me vienne en aide voici le code:
#include<stdio.h>
#include<stdlib.h>
typedef struct noeud *arbre;
typedef struct noeud
{int val;
arbre g;
arbre d;
}noeud;
typedef struct pile
{
arbre t[100];
int top;
char sens;
}pile;
int pile_pleine(pile p)
{
p.top=100;
return 0;
}
int pile_vide(pile p)
{
p.top=0;
return 0;
}
void empiler(arbre x,pile p)
{ if (!pile_pleine(p))
{
p.top=p.top+1;
p.t[p.top]=x;
}
else
{
printf("pile déja pleine");
}
}
void depiler(arbre x,pile p)
{if (pile_vide==0)
{ printf("pile deja vide");}
else
{p.top=p.top-1;}
}
void vider_pile(pile p)
{p.top=0;
}
void infixe(arbre a)
{pile s;
arbre p;
vider_pile(s);
p=a;
while ((p!=NULL)||(!pile_vide(s)==0))
{while (p!=NULL)
{empiler(a,s);
p=p->g;
}
depiler(a,s);
printf("%d\t",p->val);
p=p->d;
}
}
void prefixe(arbre a)
{pile s;
arbre p;
vider_pile(s);
p=a;
while ((p!=NULL)||(!pile_vide(s)==0))
{while (p!=NULL)
{empiler(a,s);
printf("%d\t",p->val);
p=p->g;
}
depiler(a,s);
p=p->d;
}
}
void postfixe(arbre a)
{pile s;
arbre p;
vider_pile(s);
p=a;
while ((p!=NULL)||(!pile_vide(s)==0))
{while (p!=NULL)
{empiler(a,s);
p=p->g;
}
depiler(a,s);
printf("%d\t",p->val);
p=p->d;
}
}
void ajout(int x,arbre a)
{if (a=NULL)
{
a=(arbre)malloc(1*sizeof(arbre));
a->val=x;
a->g=NULL;
a->d=NULL;
}
else
{
if(x<a->val)
{ajout(x,a->g);}
else
{ajout(x,a->d);}
}
}
int main()
{int x;
char rep;
arbre t;
do{
printf("ajouter un nombre\n");
scanf("%d",&x);
ajout(x,t);
printf("voulez-vous saisir a nouveau (o/n)\n");
}while(rep='o');
printf("affichage ave le parcours infixe\n");
infixe(t);
printf("affichage ave le parcours prefixe\n");
prefixe(t);
return 0;
}
Configuration: Linux Konqueror 3.5
2 réponses
-
Salut,
je n'ai pas étudié tes fonctions d'affichage (qui ne fonctionnent pas)
j'ai mis le commentaire en gras où j'ai modifié#include<stdio.h> #include<stdlib.h> typedef struct noeud *arbre; typedef struct noeud { int val; arbre g; arbre d; } noeud; typedef struct pile { arbre t[100]; int top; char sens; } pile; int pile_pleine (pile p) { p.top = 100; return 0; } int pile_vide (pile p) { p.top = 0; return 0; } void empiler (arbre x, pile p) { if (!pile_pleine (p)) { p.top = p.top + 1; p.t[p.top] = x; } else { printf ("pile déja pleine"); } } void depiler (arbre x, pile p) { if (pile_vide == 0) { printf ("pile deja vide"); } else { p.top = p.top - 1; } } void vider_pile (pile p) { p.top = 0; } void infixe (arbre a) { pile s; arbre p; vider_pile (s); p = a; while ((p != NULL) || (!pile_vide (s) == 0)) { while (p != NULL) { empiler (a, s); p = p->g; } depiler (a, s); printf ("%d\t", p->val); p = p->d; } } void prefixe (arbre a) { pile s; arbre p; vider_pile (s); p = a; while ((p != NULL) || (!pile_vide (s) == 0)) { while (p != NULL) { empiler (a, s); printf ("%d\t", p->val); p = p->g; } depiler (a, s); p = p->d; } } void postfixe (arbre a) { pile s; arbre p; vider_pile (s); p = a; while ((p != NULL) || (!pile_vide (s) == 0)) { while (p != NULL) { empiler (a, s); p = p->g; } depiler (a, s); printf ("%d\t", p->val); p = p->d; } } void ajout (int x, arbre a) { if (a == NULL) { /* == au lieu de = */ a = (arbre) malloc (1 * sizeof (arbre)); a->val = x; a->g = NULL; a->d = NULL; } else { if (x < a->val) { ajout (x, a->g); } else { ajout (x, a->d); } } } int main () { int x; char rep; arbre t; t = NULL; /* il faut initialiser l'arbre */ do { printf ("ajouter un nombre\n"); scanf ("%d", &x); getchar(); /* vider le buffer */ ajout (x, t); printf ("voulez-vous saisir a nouveau (o/n)\n"); scanf("%c",&rep); /* tu as oublié la saisie de rep */ } while (rep == 'o'); /* == et pas = */ printf ("affichage ave le parcours infixe\n"); infixe (t); printf ("affichage ave le parcours prefixe\n"); prefixe (t); return 0; } -
Voici un exemple, ça pourra peut être t'aider
#include<stdio.h> #include<stdlib.h> typedef struct N_ { int nb; struct N_*gauche; struct N_*droite; } Noeud; void CreerArbre (Noeud ** racine, int n); void Traiter (Noeud * racine); void AfficherPrefixe (Noeud * racine); void AfficherPostfixe (Noeud * racine); void AfficherInfixe (Noeud * racine); Noeud *alloc (); int main () { Noeud *racine; racine = NULL; int n; printf ("Numéro [100 pour terminer] ? "); scanf ("%d", &n); while (n != 100) { CreerArbre (&racine, n); printf ("Numéro [100 pour terminer] ? "); scanf ("%d", &n); } AfficherPrefixe (racine); puts (""); AfficherInfixe (racine); puts (""); AfficherPostfixe (racine); puts (""); return 0; } void CreerArbre (Noeud ** racine, int n) { Noeud *nouveau; nouveau = alloc (); if (*racine == NULL) { nouveau->nb = n; nouveau->gauche = NULL; nouveau->droite = NULL; *racine = nouveau; } else if (n < (*racine)->nb) CreerArbre (&(*racine)->gauche, n); else if (n > (*racine)->nb) CreerArbre (&(*racine)->droite, n); } Noeud *alloc () { Noeud *noeud; noeud = (Noeud *) malloc (sizeof (Noeud)); return noeud; } void Traiter (Noeud * racine) { printf ("%d (%p) ; ", racine->nb, racine); } void AfficherPrefixe (Noeud * racine) { if (racine != NULL) { Traiter (racine); AfficherPrefixe (racine->gauche); AfficherPrefixe (racine->droite); } } void AfficherPostfixe (Noeud * racine) { if (racine != NULL) { AfficherPostfixe (racine->gauche); AfficherPostfixe (racine->droite); Traiter (racine); } } void AfficherInfixe (Noeud * racine) { if (racine != NULL) { AfficherInfixe (racine->gauche); Traiter (racine); AfficherInfixe (racine->droite); } }Exécutionlami20j@debian:~/trash$ gcc arbre.c lami20j@debian:~/trash$ ./a.out Numéro [100 pour terminer] ? 15 Numéro [100 pour terminer] ? 10 Numéro [100 pour terminer] ? 26 Numéro [100 pour terminer] ? 8 Numéro [100 pour terminer] ? 56 Numéro [100 pour terminer] ? 2 Numéro [100 pour terminer] ? 42 Numéro [100 pour terminer] ? -1 Numéro [100 pour terminer] ? 100 15 (0x804a008) ; 10 (0x804a028) ; 8 (0x804a078) ; 2 (0x804a0e8) ; -1 (0x804a178) ; 26 (0x804a048) ; 56 (0x804a0a8) ; 42 (0x804a128) ; -1 (0x804a178) ; 2 (0x804a0e8) ; 8 (0x804a078) ; 10 (0x804a028) ; 15 (0x804a008) ; 26 (0x804a048) ; 42 (0x804a128) ; 56 (0x804a0a8) ; -1 (0x804a178) ; 2 (0x804a0e8) ; 8 (0x804a078) ; 10 (0x804a028) ; 42 (0x804a128) ; 56 (0x804a0a8) ; 26 (0x804a048) ; 15 (0x804a008) ;