Coursework for the Master's course Computational Data Science: Advanced Programming
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

52 lines
856 B

//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