// // Created by Julio Tentor // #ifndef DEMOQUEUE3_SPECIALQUEUE_H #define DEMOQUEUE3_SPECIALQUEUE_H template class specialqueue { public: specialqueue(int capacity = 10); virtual ~specialqueue(); void push(const ELEMENT & element); ELEMENT front(); void pop(); const int size(); specialqueue join(specialqueue & other); private: int capacity; ELEMENT * data; int head; int tail; int count; int next(int position); }; template specialqueue::specialqueue(int capacity) { this->capacity = capacity; this->data = new ELEMENT[this->capacity]; this->head = 0; this->tail = 0; this->count = 0; } template specialqueue::~specialqueue() { delete [] this->data; } template const int specialqueue::size() { return this->count; } template int specialqueue::next(int position) { return (++position >= this->capacity)? 0: position; } template void specialqueue::push(const ELEMENT &element) { if (this->count >= this->capacity) { throw std::runtime_error("ERROR La cola esta llena..."); } this->data[this->tail] = element; this->tail = this->next(this->tail); ++this->count; } template ELEMENT specialqueue::front() { if (this->count <= 0) { throw std::runtime_error("ERROR La cola esta vacía..."); } return this->data[this->head]; } template void specialqueue::pop() { if (this->count <= 0) { throw std::runtime_error("ERROR La cola esta vacía..."); } this->head = this->next(this->head); --this->count; } template specialqueue specialqueue::join(specialqueue & other) { specialqueue result = specialqueue((this->size() + other.size()) * 2); for(int pos = this->head, c = this->size(); c > 0; --c) { result.push(this->data[pos]); pos = this->next(pos); } for(int pos = other.head, c = other.size(); c > 0; --c) { result.push(other.data[pos]); pos = other.next(pos); } return result; } #endif //DEMOQUEUE3_SPECIALQUEUE_H