50 lines
705 B
C++
50 lines
705 B
C++
//Stack.h
|
|
#include <deque>
|
|
|
|
#ifndef STACK_H
|
|
#define STACK_H
|
|
|
|
template<class T>
|
|
class Stack {
|
|
// Interface
|
|
public:
|
|
Stack() {
|
|
_s = new std::deque<T>();
|
|
}
|
|
~Stack() {
|
|
delete _s;
|
|
}
|
|
|
|
int nitems() { return _s->size() ; }
|
|
bool empty() { return (_s->size() == 0) ; }
|
|
|
|
void push(T c) {
|
|
_s->push_back(c);
|
|
}
|
|
|
|
T pop() {
|
|
if (empty()) {
|
|
std::cout << "Stack::pop() Error: stack is empty" << std::endl ;
|
|
return 0 ;
|
|
}
|
|
|
|
T tmp = _s->back();
|
|
_s->pop_back();
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
void inspect() {
|
|
for ( auto iter = _s->begin(); iter != _s->end() ; iter++ )
|
|
{
|
|
std::cout << *iter << std::endl;
|
|
}
|
|
}
|
|
|
|
// Implementation
|
|
private:
|
|
std::deque<T>* _s ;
|
|
|
|
};
|
|
#endif
|