fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Macro Bool */
  5. typedef enum{false,true}bool;
  6.  
  7. /***
  8. * Structure Simple d'une Pile
  9. ***/
  10. typedef struct _Pile{
  11.  
  12. unsigned int iVal;
  13. struct _Pile *pPrec;
  14. }ts_Pile;
  15.  
  16. /***
  17. * Fonction vérification
  18. * de la pile
  19. ***/
  20. bool f_isEmpty(ts_Pile **pIn){
  21.  
  22. return ( (*pIn == NULL ) ? true : false );
  23. }
  24.  
  25. /***
  26. * Fonction qui ajoute un élement
  27. * dans la Pile
  28. ***/
  29. void f_addPile(ts_Pile **pIn, unsigned int const iVal){
  30.  
  31. ts_Pile *tmp = malloc( sizeof( ts_Pile ) );
  32. if( !tmp ){
  33. perror( "Erreur interne d'allocation\n" );
  34. exit( 1 );
  35. }
  36.  
  37. /* Ajout de l'élément */
  38. tmp->iVal = iVal;
  39. tmp->pPrec = *pIn;
  40. *pIn = tmp;
  41. }
  42.  
  43. /***
  44. * fonction qui retire un élement
  45. * de la Pile
  46. ***/
  47. unsigned const int f_PopPile(ts_Pile **pIn){
  48.  
  49. unsigned int iVal = 0;
  50.  
  51. ts_Pile *tmp = NULL;
  52. if( !*pIn )
  53. return ( -1 );
  54.  
  55. tmp = (*pIn)->pPrec;
  56. iVal = (*pIn)->iVal;
  57.  
  58. free( *pIn );
  59. *pIn = tmp;
  60.  
  61. return ( iVal );
  62. }
  63.  
  64. /***
  65. * Fonction qui libère
  66. * la mémoire allouer
  67. ***/
  68. void f_FreeAlloc( ts_Pile **pIn){
  69.  
  70. ts_Pile *tmp = NULL;
  71.  
  72. if( f_isEmpty( pIn ) != true ){
  73.  
  74. while( *pIn ){
  75. tmp = (*pIn)->pPrec;
  76. free ( *pIn );
  77. *pIn = tmp;
  78. }
  79.  
  80. *pIn = NULL;
  81. tmp = NULL;
  82. }
  83. }
  84.  
  85.  
  86. /***
  87. * Fonction principale
  88. ***/
  89. int main( void ) {
  90.  
  91. int i = 0;
  92.  
  93. /* Déclaration des piles & initialisation */
  94.  
  95. ts_Pile *pile_1 = NULL; /* Pile (1) */
  96. ts_Pile *pile_2 = NULL; /* Pile (2) */
  97. ts_Pile *pile_3 = NULL; /* Pile (3) */
  98.  
  99. /* tableau de Pointeur */
  100. ts_Pile (*pTabAddr[3]) = { &pile_1, &pile_2, &pile_3 };
  101.  
  102. /* Ajouts des valeurs à la pile */
  103. for(; i < 20 ; i++){
  104. f_addPile( pTabAddr[0], i );
  105. f_addPile( pTabAddr[1], (i*2) );
  106. f_addPile( pTabAddr[2], (i*4) );
  107. }
  108.  
  109. /* Affichage des Piles */
  110. for(i = 0; i < 20 ; i++)
  111. printf("Pile (1)\t= %d\n", f_PopPile(&pile_1) );
  112.  
  113. for(i = 0; i < 20 ; i++)
  114. printf("Pile (2)\t= %d\n", f_PopPile(&pile_2) );
  115.  
  116. for(i = 0; i < 20 ; i++)
  117. printf("Pile (3)\t= %d\n", f_PopPile(&pile_3) );
  118.  
  119.  
  120. for( i = 0; i < 20 ; i++){
  121. f_addPile( pTabAddr[0], i );
  122. f_addPile( pTabAddr[1], (i*2) );
  123. f_addPile( pTabAddr[2], (i*4) );
  124. }
  125.  
  126. /* Désalocation mémoire */
  127. f_FreeAlloc( pTabAddr[0] );
  128. f_FreeAlloc( pTabAddr[1] );
  129. f_FreeAlloc( pTabAddr[2] );
  130.  
  131. /* Affichage des Piles */
  132. for(i = 0; i < 20 ; i++)
  133. printf("Pile (1)\t= %d\n", f_PopPile(&pile_1) );
  134.  
  135. return ( 0 );
  136. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
Pile (1)	= 19
Pile (1)	= 18
Pile (1)	= 17
Pile (1)	= 16
Pile (1)	= 15
Pile (1)	= 14
Pile (1)	= 13
Pile (1)	= 12
Pile (1)	= 11
Pile (1)	= 10
Pile (1)	= 9
Pile (1)	= 8
Pile (1)	= 7
Pile (1)	= 6
Pile (1)	= 5
Pile (1)	= 4
Pile (1)	= 3
Pile (1)	= 2
Pile (1)	= 1
Pile (1)	= 0
Pile (2)	= 38
Pile (2)	= 36
Pile (2)	= 34
Pile (2)	= 32
Pile (2)	= 30
Pile (2)	= 28
Pile (2)	= 26
Pile (2)	= 24
Pile (2)	= 22
Pile (2)	= 20
Pile (2)	= 18
Pile (2)	= 16
Pile (2)	= 14
Pile (2)	= 12
Pile (2)	= 10
Pile (2)	= 8
Pile (2)	= 6
Pile (2)	= 4
Pile (2)	= 2
Pile (2)	= 0
Pile (3)	= 76
Pile (3)	= 72
Pile (3)	= 68
Pile (3)	= 64
Pile (3)	= 60
Pile (3)	= 56
Pile (3)	= 52
Pile (3)	= 48
Pile (3)	= 44
Pile (3)	= 40
Pile (3)	= 36
Pile (3)	= 32
Pile (3)	= 28
Pile (3)	= 24
Pile (3)	= 20
Pile (3)	= 16
Pile (3)	= 12
Pile (3)	= 8
Pile (3)	= 4
Pile (3)	= 0
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1
Pile (1)	= -1