#ifndef STACK_H // Fichier Stack.h #define STACK_H #include #include using namespace std; template class Stack; template // Pour les fonctions ostream& operator<<(ostream& os, const Stack& s); // friend template. // Il faut aussi <> const int MAX = 10; template class Stack { friend ostream& operator<< <>(ostream& os, const Stack& s); public : Stack(void); Stack(const Stack& s); Stack& operator=(const Stack& s); virtual ~Stack(void); bool empty(void) const; int size(void) const; const T& top(void) const; void push(const T& t); T pop(void); private : T _stack[MAX]; int _size; void _copy(const Stack& s); void _destroy(void); }; template inline Stack::Stack(void) : _size(0) {} template inline Stack::Stack(const Stack& s) { _copy(s); } template inline Stack& Stack::operator=(const Stack& s) { if (this != &s) { _destroy(); _copy(s); } return *this; } template inline Stack::~Stack(void) {} template inline bool Stack::empty(void) const { return _size == 0; } template inline int Stack::size(void) const { return _size; } template inline const T& Stack::top(void) const { assert(!empty()); return _stack[_size - 1]; } template inline void Stack::push(const T& t) { assert(_size < MAX); _stack[_size++] = t; } template inline T Stack::pop(void) { assert(!empty()); return _stack[--_size]; } template inline void Stack::_copy(const Stack& s) { _size = s._size; for(int i=0; i< _size; i++) { _stack[i] = s._stack[i]; } } template inline void Stack::_destroy(void) { } template inline ostream& operator<<(ostream& os, const Stack& s) { os << endl ; os << " Une Stack :" << endl; for(int i=s._size-1; i>=0; i--) { if (s._size==1) os << "sommet & fond : "; else if (i==s._size-1) os << " sommet : "; else if (i==0) os << " fond : "; else os << " "; os << s._stack[i]; os << endl; } os << "~~~~~~~~~~~~~~~" << endl; return os; } #endif // STACK_H