IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Probleme avec liste chainée


Sujet :

C

  1. #1
    Membre régulier Avatar de Lucas42
    Inscrit en
    Janvier 2006
    Messages
    301
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 301
    Points : 97
    Points
    97
    Par défaut Probleme avec liste chainée
    Bonjour , j'ai quelque petit soucis de compréhension avec les listes chainées ....
    même aprés avoir lu: http://nicolasj.developpez.com/articles/listesimple/
    je ne comprend pas tout ...

    Voici mon code qui permet de mettre dans un chainon une structure composant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <malloc.h>
     
    struct composant{ char NOM[20];			//Création d'une structure COMPOSANT comportant 4 éléments
    				  char FABRICANT[20];
    				  char FONCTION[100];
    				  float PRIX;
    				  composant *adresse;
    				};
     
    struct composant *ter;
     
    void CreerListeVide(void);
    void CreerListeVide(void)
    {
    	ter=NULL;
    }
     
    composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix);//,struct composant *adsuivante);
    composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix)//,struct composant *adsuivante)
    {
    	struct composant *adsuivant;
    	adsuivant = (struct composant *)malloc (sizeof(struct composant)); 
    	if (adsuivant!=NULL)												
    	{															
    		strcpy((*adsuivant).NOM , Inom);
    		strcpy((*adsuivant).FABRICANT , Ifabricant);
    		strcpy((*adsuivant).FONCTION , Ifonction);
    		(*adsuivant).PRIX = Iprix;
    		//(*ad).adresse = adsuivante;
    		//adsuivante = ad;
    		ter = adsuivant;
    	}
    	return *adsuivant;
    }
     
    void main(void)
    {
    	int choix;
     
    	char Snom[20];
    	char Sfabricant[20];
    	char Sfonction[100];
    	float Sprix;
     
    	CreerListeVide();
     
            printf("\nSaisir Nom du composant\n");
    	gets(Snom);
    	printf("Saisir Fabricant du composant\n");
    	gets(Sfabricant);
    	printf("Saisir Fonction du composant\n");
    	gets(Sfonction);
    	printf("Saisir Prix du composant\n");
    	scanf("%f",&Sprix);
    	InsertionEnAd(Snom,Sfabricant,Sfonction,Sprix);//,ter);
    }
    Donc avec ce code je met dans le premier maillon :
    Snom
    Sfabricant
    Sfonction
    Sprix
    et j'aimerai refaire cette manip et mettre :
    Snom
    Sfabricant
    Sfonction
    Sprix
    dans le second maillon ... ainsi de suite...

    donc si vous pouvez eclairer ma lanterne j'apprecierai beaucoup

    Amicalement lucas
    AMis programmeurs

  2. #2
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Quelques remarques :
    - tels que placés, les prototypes sont parfaitement inutiles
    - il n'y a aucune raison que ter soit global
    - (*adsuivant).NOM est avantageusement remplaçable par adsuivant->NOM
    - main renvoie toujours int

    - personnellement, je construirai une fonction CreerComposant qui me donnerai un pointeur sur la structure composant construite ou NULL si c'est impossible
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    struct composant * CreerComposant (char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix);
    puis une fonction d'insertion dans une liste
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ??? InsertComposant(struct composant * liste ,struct composant * composant);
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    int main(void)
    {
      struct composant * p;
      struct composant * ter = NULL;
    ....
       p = CreerComposant (...);
       if(p!= NULL)
          InsertComposant(ter,p);
    ....
    }

  3. #3
    Membre régulier Avatar de Lucas42
    Inscrit en
    Janvier 2006
    Messages
    301
    Détails du profil
    Informations forums :
    Inscription : Janvier 2006
    Messages : 301
    Points : 97
    Points
    97
    Par défaut
    donc si j'ai bien compris il faut :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    InsertComposant(struct composant * liste ,struct composant * composant);
    InsertComposant(struct composant * liste ,struct composant * composant)
    {
    }
     
    struct composant *CreerComposant (char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix);//,struct composant *adsuivante);
    struct composant *CreerComposant (char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix)//,struct composant *adsuivante)
    {
    	struct composant *adsuivant;
    	adsuivant = (struct composant *)malloc (sizeof(struct composant)); 
    	if (adsuivant!=NULL)												
    	{															
    		strcpy((*adsuivant).NOM , Inom);
    		strcpy((*adsuivant).FABRICANT , Ifabricant);
    		strcpy((*adsuivant).FONCTION , Ifonction);
    		(*adsuivant).PRIX = Iprix;
    		//(*ad).adresse = adsuivante;
    		//adsuivante = ad;
    		ter = adsuivant;
    	}
    	return *adsuivant;
    }
     
    int main(void)
    {
      struct composant * p;
      struct composant * ter = NULL;
    int choix;
     
    	char Snom[20];
    	char Sfabricant[20];
    	char Sfonction[100];
    	float Sprix;
     
            printf("\nSaisir Nom du composant\n");
    	gets(Snom);
    	printf("Saisir Fabricant du composant\n");
    	gets(Sfabricant);
    	printf("Saisir Fonction du composant\n");
    	gets(Sfonction);
    	printf("Saisir Prix du composant\n");
    	scanf("%f",&Sprix);
            p = CreerComposant (char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix);
            if(p!= NULL)
            InsertComposant(ter,p);
    ....
    }
    mais je vois pas trop
    ma fonction
    CreerComposant
    met directement Snom .... Sprix dans le premier maillon , que mettre dans la fonction
    InsertComposant
    ???

    et mon code en sa globalité :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
     
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <malloc.h>
     
    struct composant{ char NOM[20];			//Création d'une structure COMPOSANT comportant 4 éléments
    				  char FABRICANT[20];
    				  char FONCTION[100];
    				  float PRIX;
    				  composant *adresse;
    				};
     
    struct composant * ter = NULL;
    /*void CreerListeVide(void);
    void CreerListeVide(void)
    {
    	ter=NULL;
    }*/
     
    composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix);//,struct composant *adsuivante);
    composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix)//,struct composant *adsuivante)
    {
    	struct composant *adsuivant;
    	adsuivant = (struct composant *)malloc (sizeof(struct composant)); 
    	struct composant *adapres;
    	adapres = (struct composant *)malloc (sizeof(struct composant)); 
    	if (adsuivant!=NULL)												
    	{															
    		strcpy((*adsuivant).NOM , Inom);
    		strcpy((*adsuivant).FABRICANT , Ifabricant);
    		strcpy((*adsuivant).FONCTION , Ifonction);
    		(*adsuivant).PRIX = Iprix;
    		//(*ad).adresse = adsuivante;
    		//adsuivante = ad;
    		ter = adsuivant;
    		adapres=NULL;
    	}
    	return *adsuivant;
    }
     
    void Afficher(struct composant *Aad);
    void Afficher(struct composant *Aad)
    {//do{
    	if(Aad!=NULL)
    		printf("\nNom : %s\nFabricant : %s\nFonction : %s\nPrix HT : %f Euros\n",Aad->NOM,Aad->FABRICANT,Aad->FONCTION,Aad->PRIX);
    	else
    		printf("         !Aucun composant(s) present dans la liste chaine!     \n");
    //}while(Aad!=NULL);
    }
     
    void SauvegarderTexte(struct composant *Sad);
    void SauvegarderTexte(struct composant *Sad)
    {
    	FILE *Fich;
    	int fermer=0;
     
    	/* Ouverture pour ecriture seule*/
    	if((Fich = fopen( "c:\\fichier.txt", "a" )) == NULL ) // "a" permet d'ajouter de nouveau element en fin de fichier
    		printf("-----------------Le Fichier na pas pu etre ouvert-----------------\n" );
        else
    	{
    		printf("----------------------Le Fichier est ouvert-----------------------\n" );
     
    		/* Ecriture */
    		if(Sad!=NULL)
    		{
    			fprintf(Fich,"------------------\n");
    			fprintf(Fich,"Nom : %s\n",Sad->NOM);
    			fprintf(Fich,"Fabricant : %s\n",Sad->FABRICANT);
    			fprintf(Fich,"Fonction : %s\n",Sad->FONCTION);
    			fprintf(Fich,"Prix HT : %f Euros\n",Sad->PRIX);
    			fprintf(Fich,"------------------");
    			printf("\n                       SAUVEGARDE EFFECTUE                        \n" );
    		}
    		else
    			printf("\n         !Aucun composant(s) present dans la liste chaine!     \n");
     
    	/* Fermeture */
    	}
        fermer = _fcloseall();
    	fclose(Fich);
    	if(fermer>0)
    		printf("\n--------------------------Fichier Fermer--------------------------\n");
    	else
    		printf("\n------------------------Fichier NON Fermer------------------------\n");
    }
     
    void AfficherTexte();
    void AfficherTexte()
    {
    	FILE *Fich;
    	char INPUT[255]; 
    	int fermer=0,i,c;
     
    	/* Ouverture pour lecture seule*/
    	if((Fich = fopen( "c:\\fichier.txt", "r" )) == NULL )
    		printf("-----------------Le Fichier na pas pu etre ouvert-----------------\n" );
        else
    	{
    		printf("----------------------Le Fichier est ouvert-----------------------\n" );
     
    		/* Lecture */
    		if((c=fgetc(Fich)) != EOF)
    			{
    				printf("------------------------CONTENU DU FICHIER------------------------\n" );
    				while (!feof(Fich))// si on est pas arrivé a la fin
    					{
    						fgets(INPUT, 255, Fich); //On range la chaine dans INPUT
    						printf("%s",INPUT);
    						//fprintf(stdout,"%s",INPUT);
    					}
    			}
    		else
    			{
    				printf("\n                      !LE FICHIER EST VIDE!                    \n");
    			}
    		/* Fermeture */
     
    		fermer = _fcloseall();
    		fclose(Fich);
    		if(fermer>0)
    			printf("\n--------------------------Fichier Fermer--------------------------\n");
    		else
    			printf("\n------------------------Fichier NON Fermer------------------------\n");
    	}
    }
     
    int main(void)
    {
    	struct composant * p;
     
    	int choix;
     
    	char Snom[20];
    	char Sfabricant[20];
    	char Sfonction[100];
    	float Sprix;
     
    	//CreerListeVide();
     
    	do{
    	    printf("\n-----------------------------COMMANDES----------------------------\n");
    		printf("- 1 : Entrer un nouveau composant                                -\n");
    		printf("- 2 : Afficher le composant                                      -\n");
    		printf("- 3 : Afficher tous les composants                               -\n");
    		printf("- 4 : Sauvegarder la liste de composant dans un fichier texte    -\n");
    		printf("- 5 : Afficher tous les composants contenu dans le fichier texte -\n");
    		printf("- 6 : QUITTER                                                    -\n");
    		printf("------------------------------------------------------------------\n");
    		scanf("%d",&choix);
    		scanf("%*[^\n]"),getchar();
     
    		if(choix==1)
    		{
    			printf("\nSaisir Nom du composant\n");
    			gets(Snom);
    			printf("Saisir Fabricant du composant\n");
    			gets(Sfabricant);
    			printf("Saisir Fonction du composant\n");
    			gets(Sfonction);
    			printf("Saisir Prix du composant\n");
    			scanf("%f",&Sprix);
    			//if(p
    			InsertionEnAd(Snom,Sfabricant,Sfonction,Sprix);//,ter);
    		}
    		if(choix==2)
    		{
    			Afficher(ter);
    		}
    		/*if(choix==3)
    		{
    			AfficherTous(ter);
    		}*/
    		if(choix==4)
    		{
    			SauvegarderTexte(ter);
    		}
    		if(choix==5)
    		{
    			AfficherTexte();
    		}
    	}while(choix!=6);
     
    }

  4. #4
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Pour les prototypes : en haut (ou dans un .h à inclure)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix);
    void Afficher(struct composant *Aad);
    void SauvegarderTexte(struct composant *Sad);
    ....
    composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix)
    {
    .....
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    struct composant *CreerComposant (char *Inom,char *Ifabricant,char* Ifonction,float Iprix)
    {
    	struct composant *adsuivant;
    	adsuivant = malloc (sizeof(struct composant)); 
    	if (adsuivant!=NULL)
            {												
    		strcpy(adsuivant->NOM , Inom);
    		strcpy(adsuivant->FABRICANT , Ifabricant);
    		strcpy(adsuivant->FONCTION , Ifonction);
    		adsuivant->PRIX = Iprix;
    		adsuivant->adresse = NULL;
    	}
    	return adsuivant;
    }
    La fonction a créé une cellule. La partie suivante est de l'insérer dans une liste donnée

Discussions similaires

  1. probleme avec liste chainée
    Par isoman dans le forum C
    Réponses: 14
    Dernier message: 29/11/2006, 23h03
  2. probleme avec liste chainée
    Par Liiscar dans le forum Collection et Stream
    Réponses: 3
    Dernier message: 28/11/2006, 20h37
  3. [MySQL] probleme avec liste déroulante et requete
    Par Ludo75 dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 26/01/2006, 13h55
  4. Probleme arbre/liste chainée en template
    Par Raton dans le forum Langage
    Réponses: 1
    Dernier message: 07/11/2005, 16h09
  5. Mal a la tete avec liste chainée d'objet
    Par Raton dans le forum C++
    Réponses: 23
    Dernier message: 03/08/2005, 22h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo