// // Created by Julio Tentor // #ifndef DEMOQUEUE1_QUEUE_H #define DEMOQUEUE1_QUEUE_H #include template class queue { public: queue(int capacity = 10); virtual ~queue(); void push(const ELEMENT & element); ELEMENT front(); void pop(); const int size(); private: int capacity; ELEMENT * data; int head; int tail; int count; int next(int position); }; template queue::queue(int capacity) { this->capacity = capacity; this->data = new ELEMENT[this->capacity]; this->head = 0; this->tail = 0; this->count = 0; } template queue::~queue() { delete [] this->data; } template const int queue::size() { return this->count; } template int queue::next(int position) { return (++position >= this->capacity)? 0: position; } template void queue::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 queue::front() { if (this->count <= 0) { throw std::runtime_error("ERROR La cola esta vacía..."); } return this->data[this->head]; } template void queue::pop() { if (this->count <= 0) { throw std::runtime_error("ERROR La cola esta vacía..."); } this->head = this->next(this->head); --this->count; } #endif //DEMOQUEUE1_QUEUE_H