Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
- //
- #ifndef DEMOQUEUE1_QUEUE_H
- #define DEMOQUEUE1_QUEUE_H
- #include <stdexcept>
- template <class ELEMENT>
- 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 <class ELEMENT>
- queue<ELEMENT>::queue(int capacity) {
- this->capacity = capacity;
- this->data = new ELEMENT[this->capacity];
- this->head = 0;
- this->tail = 0;
- this->count = 0;
- }
- template <class ELEMENT>
- queue<ELEMENT>::~queue() {
- delete [] this->data;
- }
- template <class ELEMENT>
- const int queue<ELEMENT>::size() {
- return this->count;
- }
- template<class ELEMENT>
- int queue<ELEMENT>::next(int position) {
- return (++position >= this->capacity)? 0: position;
- }
- template<class ELEMENT>
- void queue<ELEMENT>::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<class ELEMENT>
- ELEMENT queue<ELEMENT>::front() {
- if (this->count <= 0) {
- throw std::runtime_error("ERROR La cola esta vacía...");
- }
- return this->data[this->head];
- }
- template<class ELEMENT>
- void queue<ELEMENT>::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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement