53 lines
856 B
C++
53 lines
856 B
C++
|
//Stack.h
|
||
|
#include "Array.hh"
|
||
|
|
||
|
#ifndef STACK_H
|
||
|
#define STACK_H
|
||
|
|
||
|
template<class T>
|
||
|
class Stack {
|
||
|
// Interface
|
||
|
public:
|
||
|
Stack( int in_size ) {
|
||
|
count = 0;
|
||
|
_s = new Array<T>(in_size, 0);
|
||
|
}
|
||
|
|
||
|
int nitems() { return count ; }
|
||
|
bool full() { return (count == _s->size()) ; }
|
||
|
bool empty() { return (count == 0) ; }
|
||
|
|
||
|
void push(T c) {
|
||
|
if (full()) {
|
||
|
std::cout << "Stack::push() Error: stack is full" << std::endl ;
|
||
|
|
||
|
_s->resize( _s->size() + 10 );
|
||
|
}
|
||
|
|
||
|
_s[0][count++] = c ;
|
||
|
}
|
||
|
|
||
|
T pop() {
|
||
|
if (empty()) {
|
||
|
std::cout << "Stack::pop() Error: stack is empty" << std::endl ;
|
||
|
return 0 ;
|
||
|
}
|
||
|
|
||
|
return _s[0][--count] ;
|
||
|
}
|
||
|
|
||
|
void inspect() {
|
||
|
for ( int i = nitems() - 1; i >= 0; i-- )
|
||
|
{
|
||
|
std::cout << i << ": " << _s[0][i] << std::endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Implementation
|
||
|
private:
|
||
|
Array<T>* _s ;
|
||
|
int count ;
|
||
|
|
||
|
};
|
||
|
#endif
|