Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- // LA STRUCTURE :
- typedef struct ElementListe{
- int info;
- struct ElementListe *svt;
- } element;
- typedef struct ListeRepere{
- element *debut;
- int taille;
- } pile;
- // 1 INITIALISATION :
- pile init_pile(pile *p){
- p->debut=NULL;
- p->taille=0;
- }
- // 2 EMPILER
- void empiler(pile *p, int x){
- element *nouv;
- nouv=(element*)malloc(sizeof(element));
- nouv->info=x;
- nouv->svt=p->debut;
- p->debut=nouv;
- p->taille++;
- }
- // 3 DEPILER
- void depiler(pile *p)
- { element *mp;
- if(p->taille==0){printf("pas d element a depiler .");
- }
- else{
- mp=p->debut;
- p->debut=p->debut->svt;
- free(mp);
- p->taille--;}
- }
- // 4 SOMMET PILE
- int voir_sommet(pile *p){
- if(p!=NULL)
- return(p->debut->info);
- }
- // 5 PILE VIDE
- void pile_vide(pile *p){
- if(p->debut!=NULL){printf("LA PILE N EST PAS VIDE.\n");
- }
- else{printf("LA PILE EST VIDE.\n");
- }
- }
- main(){
- pile *p;
- int x,sommet;
- //INITIALISATION
- init_pile(p);
- //TEST SI C VIDE
- pile_vide(p);
- // remplire la pile
- printf("donnez n : ");
- scanf("%d",&x);
- while(x!=(-1)){
- empiler(p,x);
- printf("donnez -1 pour arreter.\n");
- printf("donnez n : ");
- scanf("%d",&x);
- }
- // SOMMET PILE
- sommet=voir_sommet(p);
- printf("le sommet de cette pile est : %d\n",sommet);
- // DEPILER
- depiler(p);
- // SOMMET PILE
- sommet=voir_sommet(p);
- printf("le sommet de cette pile est : %d\n",sommet);
- // VIDER LE PILE
- while(p->debut!=NULL){
- depiler(p);
- }
- // TEST SI VIDE
- pile_vide(p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement