1 #include <iostream>
   2 #include <cstdlib>
   3 using namespace std;
   4 
   5 
   6 class stack { 
   7         
   8 public:
   9         stack(int n) : size(n), top(0) {
  10                 A = (int *) malloc(size*sizeof(int));
  11                 for (int i=0; i<size; i++) A[i] = 0;
  12         };
  13         ~stack() { free(A); };
  14         void push (int);
  15         int pop();
  16         int length() const {return top;};
  17         
  18 private:
  19         const int size;
  20         int *A;
  21         int top;
  22 };
  23 
  24 void stack::push (int i) {
  25         if (top < size) {
  26                 A[top++] = i;
  27         }
  28 }
  29 
  30 int stack::pop() {
  31         if (top) {
  32                 return A[--top];
  33         } else {
  34                 return 0;
  35         }
  36 }
  37 
  38 
  39 int main() {
  40         stack s(10);
  41         srand(time(NULL));
  42         
  43         for (int i=0;i<15;i++)
  44         {
  45                 int z = rand() % 1000;
  46                 s.push(z);
  47                 cout << "je mets dans la pile le nombre " << z << " (taille de la pile " << s.length() << ")\n";
  48         }
  49         cout << "\n\n\n";
  50         for (int i=0; i<15; i++) {
  51                 cout << "je depile maintenant le nombre " << s.pop() << " (taille de la pile " << s.length() << ")\n";
  52         }
  53 }
  54 
  55