#include #include "stack-ve.h" #include "stack-ve_iterator.h" namespace StackLib { //-------------------------------------------- // Construtor //-------------------------------------------- StackVE::StackVE(int size) : Stack() { std::cout<<"\n Construindo pilha - vetor\n"; data_ = new int[size]; topIndex_ = -1; this->size_ = size; } //-------------------------------------------- // Destructor //-------------------------------------------- StackVE::~StackVE() { std::cout<<"\n Removendo pilha - vetor\n"; delete [] data_; } //-------------------------------------------- // Stack Methods //-------------------------------------------- void StackVE::push(int n) { if( isFull() ) { // reallocate data vector !!! int *data2 = new int[2*size_]; for(int i=0; i<= topIndex_; ++i) { data2[i] = data_[i]; } delete [] data_; size_ *= 2; data_ = data2; } data_[++topIndex_] = n; } int StackVE::pop() { if( isEmpty() ) { std::cerr<<"\nErro: nao pode fazer pop em pilha vazia"; return -99999; //exit(-1); } return data_[topIndex_--]; } int StackVE::top() { if( isEmpty() ) { std::cerr<<"\nErro: nao pode fazer top em pilha vazia"; return -99999; //exit(-1); } return data_[topIndex_]; } //-------------------------------------------- // Iterator Methods //-------------------------------------------- StackIterator *StackVE::iterator() { return new StackVEIterator(this); } }