#include #include "stack.h" Elem::Elem() : next(NULL),prev(NULL) {} Elem::~Elem() { if(NULL != prev || NULL != next) { prev->next = next; next->prev = prev; } } int Elem::make_tail(Elem& tail) { int rv(1); if(NULL != next || NULL != tail.prev) { cout << "Trying to make a head tail relationship out of"; cout << "inappropriate endpoints"; rv = 0; } else { prev = NULL; next = &tail; tail.prev = this; tail.next = NULL; } return(rv); } Stack::Stack(int num): size(num),count(0),head(-1), tail(-1) { array = new int[size]; } Stack::~Stack() { delete[] array; cout <<"destructor called for a stack of size" << size << "\n"; } //we may need to order the data here later, will check to see if //it takes to long to decide this int Stack::push(const int new_elem) { int rv(1); if(count == size) { cout <<"can't add to a full stack\n"; rv = 0; } else { int pos; pos =(tail + 1) % size; array[pos] = new_elem; tail = pos; count++; } return(rv); } int Stack::pop() { int rv(-1); if(0 == count) { cout <<"can't add to a full stack\n"; } else { rv = array[head]; array[head] = -1; head =(head + 1) % size; count--; } return(rv); } int Stack::is_in(const int& elem) { int i = head; int rv(0); while(i != tail || 1 == rv) { if(elem == array[i]) rv = 1; else i = (i+1) % size; } return(rv); }