Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. // LA STRUCTURE :
  5. typedef struct ElementListe{
  6. int info;
  7. struct ElementListe *svt;
  8. } element;
  9.  
  10. typedef struct ListeRepere{
  11. element *debut;
  12. int taille;
  13. } pile;
  14.  
  15.  
  16.  
  17. // 1 INITIALISATION :
  18. pile init_pile(pile *p){
  19. p->debut=NULL;
  20. p->taille=0;
  21. }
  22.  
  23.  
  24.  
  25. // 2 EMPILER
  26. void empiler(pile *p, int x){
  27.  
  28. element *nouv;
  29. nouv=(element*)malloc(sizeof(element));
  30. nouv->info=x;
  31. nouv->svt=p->debut;
  32. p->debut=nouv;
  33. p->taille++;
  34. }
  35.  
  36.  
  37. // 3 DEPILER
  38. void depiler(pile *p)
  39. { element *mp;
  40. if(p->taille==0){printf("pas d element a depiler .");
  41. }
  42. else{
  43.  
  44. mp=p->debut;
  45. p->debut=p->debut->svt;
  46. free(mp);
  47. p->taille--;}
  48. }
  49.  
  50. // 4 SOMMET PILE
  51. int voir_sommet(pile *p){
  52. if(p!=NULL)
  53. return(p->debut->info);
  54. }
  55.  
  56. // 5 PILE VIDE
  57. void pile_vide(pile *p){
  58. if(p->debut!=NULL){printf("LA PILE N EST PAS VIDE.\n");
  59. }
  60. else{printf("LA PILE EST VIDE.\n");
  61. }
  62.  
  63. }
  64.  
  65.  
  66. main(){
  67. pile *p;
  68. int x,sommet;
  69. //INITIALISATION
  70. init_pile(p);
  71.  
  72. //TEST SI C VIDE
  73. pile_vide(p);
  74.  
  75.  
  76. // remplire la pile
  77. printf("donnez n : ");
  78. scanf("%d",&x);
  79.  
  80. while(x!=(-1)){
  81. empiler(p,x);
  82. printf("donnez -1 pour arreter.\n");
  83. printf("donnez n : ");
  84. scanf("%d",&x);
  85. }
  86.  
  87. // SOMMET PILE
  88.  
  89. sommet=voir_sommet(p);
  90. printf("le sommet de cette pile est : %d\n",sommet);
  91.  
  92. // DEPILER
  93. depiler(p);
  94. // SOMMET PILE
  95.  
  96. sommet=voir_sommet(p);
  97. printf("le sommet de cette pile est : %d\n",sommet);
  98.  
  99.  
  100. // VIDER LE PILE
  101. while(p->debut!=NULL){
  102. depiler(p);
  103. }
  104.  
  105. // TEST SI VIDE
  106. pile_vide(p);
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement