Piles en C
glee89
Messages postés
21
Date d'inscription
Statut
Membre
Dernière intervention
-
glee89 Messages postés 21 Date d'inscription Statut Membre Dernière intervention -
glee89 Messages postés 21 Date d'inscription Statut Membre Dernière intervention -
Programme:
Bibliothèque:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include "pile.h" char affi_menu() { printf("/*****************************************************************************\\n") ; printf("\n * *\n") ; printf("\n * GESTION D'UNE PILE DE PERSONNES *\n") ; printf("\n * *\n") ; printf("/*****************************************************************************\\n") ; printf("\n \n") ; } int menu () { printf("\n 1 - Insertion dans la pile\n") ; printf("\n 2 - Retrait de la pile\n") ; printf("\n 3 - Listage de la pile\n") ; printf("\n 4 - Modification dans la pile\n") ; printf("\n 5 - Suppression dans la pile\n") ; printf("\n 0 - Fin\n") ; printf("\n") ; printf(" Validez votre choix: ") ; int cod ; scanf ("%d", &cod) ; getchar() ; printf ("\n") ; return cod ; } void initialisation (Pile *ptr_tas){ ptr_tas->debut = NULL ; ptr_tas->taille = 0 ; } /* empiler (ajouter) un nom */ int empiler (Pile * ptr_tas, char *donnee){ Cellule*nouveau_element ; if ((nouveau_element = (Cellule*) malloc (sizeof(Cellule))) == NULL) return -1 ; if ((nouveau_element->info = (char*) malloc (50 * sizeof(char)))== NULL) return -1 ; strcpy (nouveau_element->info, donnee) ; nouveau_element->suiv = ptr_tas->debut ; ptr_tas->debut = nouveau_element ; ptr_tas->taille++ ; } /* depiler seulement un nom */ int depiler (Pile * ptr_tas){ Cellule*supp_element ; if (ptr_tas->taille == 0) return -1 ; supp_element = ptr_tas->debut ; ptr_tas->debut = ptr_tas->debut->suiv ; free (supp_element->info) ; free (supp_element) ; ptr_tas->taille-- ; return 0 ; } /* affichage de la pile */ void affiche (Pile*ptr_tas){ Cellule*courant ; int i ; courant = ptr_tas->debut ; for(i=0 ; i<ptr_tas->taille ; ++i) { printf("\t \t \t \t %s\n", courant->info) ; courant = courant->suiv ; } } /* modifier un nom dans la pile */ int modifier(Pile*ptr_tas) { int trouve=0, k=0, j ; char*Nom_Mod ; Pile*pt_inter ; Cellule*otre_cel ; Cellule*cel_inter ; Cellule*auxi ; pt_inter = (Pile*)malloc(sizeof(Pile)) ; cel_inter = (Cellule*) malloc (sizeof(Cellule)) ; otre_cel = (Cellule*)malloc(sizeof(Cellule)) ; auxi = (Cellule*)malloc(sizeof(Cellule)) ; if (ptr_tas->taille == 0) return -1 ; initialisation(pt_inter) ; Nom_Mod = (char*)malloc(sizeof(char)) ; printf("Entez le nom a modifier: ") ; scanf("%s",Nom_Mod) ; while(!trouve) { cel_inter = ptr_tas->debut ; ptr_tas->debut = ptr_tas->debut->suiv ; ptr_tas->taille-- ; if(strcmp(cel_inter->info, Nom_Mod) != 0) { printf("\n %s n'est pas le nom a modifier.\n",cel_inter->info) ; strcpy(otre_cel->info, cel_inter->info) ; otre_cel->suiv = pt_inter->debut ; pt_inter->debut = otre_cel ; pt_inter->taille++ ; k++ ; trouve ; } else { trouve = 1 ; printf("\n%s est a modifier.\n",cel_inter->info) ; printf("\nModifiez le nom: ") ; scanf("%s",Nom_Mod) ; strcpy(cel_inter->info, Nom_Mod) ; cel_inter->suiv = ptr_tas->debut ; ptr_tas->debut = cel_inter ; ptr_tas->taille++ ; for(j=k ; j>0 ; j--) { auxi = pt_inter->debut ; pt_inter->debut = pt_inter->debut->suiv ; pt_inter->taille-- ; strcpy(otre_cel->info, auxi->info) ; otre_cel->suiv = ptr_tas->debut ; ptr_tas->debut = otre_cel ; ptr_tas->taille++ ; } break ; } if(ptr_tas->taille == 0) { printf("\n %s n'existe pas dans la pile.\n",Nom_Mod) ; break ; } } getch() ; } /* depiler et supprimer un nom de la pile */ int supprimer (Pile*ptr_tas){ int arret = 0 ; Cellule*Supp_Nom ; char*Nom_a_Sup ; Nom_a_Sup = (char*)malloc(sizeof(char)) ; if (ptr_tas->taille == 0) return -1 ; printf("Donner le nom a supprimer: ") ; scanf("%s", Nom_a_Sup) ; while(!arret) { Supp_Nom = ptr_tas->debut ; ptr_tas->debut = ptr_tas->debut->suiv ; ptr_tas->taille-- ; if(strcmp(Supp_Nom->info, Nom_a_Sup) != 0) { printf("\nOn depile [%s]\n",Supp_Nom->info) ; arret ; } else { printf("\nEnfin on supprime [%s] de la pile\n",Supp_Nom->info) ; arret = 1 ; free(Supp_Nom->info) ; free(Supp_Nom) ; break ; } if(ptr_tas->taille == 0) { printf("\n %s n'existe pas dans la pile.\n",Nom_a_Sup) ; break ; } } return 0 ; getch() ; } int main () { int stop = 0 ; Pile *ptr_tas ; char *ptr_nom ; if ((ptr_tas = (Pile *) malloc (sizeof (Pile))) == NULL) return -1 ; if ((ptr_nom = (char *) malloc (50 * sizeof (char))) == NULL) return -1 ; initialisation (ptr_tas) ; while (!stop) { // tantque (nonstop) faire affi_menu() ; switch (menu() ) { case 0 : system("cls") ; affi_menu() ; printf("\t Bye!!\n") ; stop = 1 ; break ; case 1: system("cls") ; affi_menu() ; printf ("Entrez un nom : ") ; scanf ("%s", ptr_nom) ; empiler (ptr_tas, ptr_nom) ; printf ("La pile contient %d elements. \n",ptr_tas->taille) ; printf("\n \n") ; system("pause") ; system("cls") ; break ; case 2: system("cls") ; if (ptr_tas->taille==0){ affi_menu() ; printf("\nRetrait interdit.\n") ; printf("\nLa pile est vide!\n") ; } else{ affi_menu() ; printf ("\nLe dernier entre (LastInFirstOut) [ %s ] sera supprime",pile_donnee(ptr_tas)) ; printf ("\nLe dernier entre est supprime\n") ; depiler (ptr_tas) ; /* suppression de dernier element entre */ printf ("La pile contient %d elements. \n",ptr_tas->taille) ; } printf("\n \n") ; system("pause") ; system("cls") ; break ; case 3: system("cls") ; affi_menu() ; printf("\n************ Haut de la PILE *************\n") ; affiche(ptr_tas) ; printf("\n____________ Bas de la PILE___________\n\n") ; printf(" \n") ; printf("\n \n") ; system("pause") ; system("cls") ; break ; case 4: system("cls") ; affi_menu() ; modifier(ptr_tas) ; system("pause") ; system("cls") ; break ; case 5: system("cls") ; affi_menu() ; supprimer(ptr_tas) ; system("pause") ; system("cls") ; break ; } // fin_switch() } // fin_tantque printf(" \n") ; system("pause") ; return 0 ; getch() ; }
Bibliothèque:
typedef struct ElementListe{ char *donnee ; struct ElementListe *suiv ; } Element ; typedef struct ListeRepere{ Element *debut ; int taille ; } Pile ; /* initialisation */ void initialisation (Pile *ptr_tas) ; /* EMPILER*/ int empiler (Pile *ptr_tas, char *donnee) ; /* DEPILER*/ int depiler (Pile *ptr_tas) ; /* Affichage de élément en haut de la pile (LastInFirstOut) */ #define pile_donnee(ptr_tas) ptr_tas->debut->donnee /* Affiche la pile */ void affiche (Pile *ptr_tas) ;
3 réponses
Le programme se plante lorsqu'on veut modifier un nom qui n'est pas au sommet de la pile.
Une aide svp!
Une aide svp!
Bonjour,
Tu pouvais continuer sur ton ancien post.
C'était plus simple.
Sinon, pareil, j'ai pas tout lu.
Mais : int empiler (Pile * ptr_tas, char *donnee){
Il faut mettre Pile** ptr_tas et bien sûr répercuter le changement dans le code de la fonction.
Et pour l'appel de la fonction : empiler(&ptr_tas, ...);
Cdlt,
Google is your friend
Tu pouvais continuer sur ton ancien post.
C'était plus simple.
Sinon, pareil, j'ai pas tout lu.
Mais : int empiler (Pile * ptr_tas, char *donnee){
Il faut mettre Pile** ptr_tas et bien sûr répercuter le changement dans le code de la fonction.
Et pour l'appel de la fonction : empiler(&ptr_tas, ...);
Cdlt,
Google is your friend
Quel sera le changement dans la fonction? Je veux juste comprendre le but des deux étoiles.