Advertisement
FiddleComputers

Dynamic stack essentials C

Jan 12th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct noeud
  5. {
  6.     int donnees;
  7.     struct noeud *suivant;
  8. };
  9.  
  10. struct noeud *pile = NULL;
  11.  
  12.  
  13. void empiler(int element)
  14. {
  15.     struct noeud *nptr = malloc(sizeof(struct noeud));
  16.     nptr->donnees = element;
  17.     nptr->suivant = pile;
  18.     pile = nptr;
  19. }
  20.  
  21. void afficher()
  22. {
  23.     struct noeud *temp;
  24.     temp = pile;
  25.     while (temp != NULL)
  26.     {
  27.         printf("\n%d", temp->donnees);
  28.         temp = temp->suivant;
  29.     }
  30. }
  31.  
  32. void depiler()
  33. {
  34.     if (pile == NULL)
  35.     {
  36.         printf("\n\nLa pile est vide ");
  37.     }
  38.     else
  39.     {
  40.         struct noeud *temp;
  41.         temp = pile;
  42.         pile = pile->suivant;
  43.         printf("\n\n%d supprime", temp->donnees);
  44.         free(temp);
  45.     }
  46. }
  47.  
  48. int main()
  49. {
  50.     int n, ch;
  51.     do
  52.     {
  53.         printf("\n\nMenu Pile\n1. Empiler \n2. Depiler\n3. Afficher\n0. Quittez");
  54.         printf("\nEntrer votre choix 0-3 ? : ");
  55.         scanf("%d", &ch);
  56.         switch (ch)
  57.         {
  58.             case 1:
  59.                 printf("\nEntrer un nombre ");
  60.                 scanf("%d", &n);
  61.                 empiler(n);
  62.                 break;
  63.             case 2:
  64.                 depiler();
  65.                 break;
  66.             case 3:
  67.                 afficher();
  68.                 break;
  69.         }
  70.     }while (ch != 0);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement