1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex6.3/Stack.hh

53 lines
856 B
C++
Raw Normal View History

2020-01-06 13:28:39 +01:00
//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