Advertisement
jtentor

DemoQueue3 - SpecialQueue.h

May 17th, 2020
648
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  3. //
  4.  
  5. #ifndef DEMOQUEUE3_SPECIALQUEUE_H
  6. #define DEMOQUEUE3_SPECIALQUEUE_H
  7.  
  8. template <class ELEMENT>
  9. class specialqueue {
  10. public:
  11.     specialqueue(int capacity = 10);
  12.     virtual ~specialqueue();
  13.     void push(const ELEMENT & element);
  14.     ELEMENT front();
  15.     void pop();
  16.     const int size();
  17.     specialqueue<ELEMENT> join(specialqueue<ELEMENT> & other);
  18.  
  19. private:
  20.     int capacity;
  21.     ELEMENT * data;
  22.     int head;
  23.     int tail;
  24.     int count;
  25.     int next(int position);
  26. };
  27.  
  28. template <class ELEMENT>
  29. specialqueue<ELEMENT>::specialqueue(int capacity) {
  30.     this->capacity = capacity;
  31.     this->data = new ELEMENT[this->capacity];
  32.     this->head = 0;
  33.     this->tail = 0;
  34.     this->count = 0;
  35. }
  36.  
  37. template <class ELEMENT>
  38. specialqueue<ELEMENT>::~specialqueue() {
  39.     delete [] this->data;
  40. }
  41.  
  42. template <class ELEMENT>
  43. const int specialqueue<ELEMENT>::size() {
  44.     return this->count;
  45. }
  46.  
  47. template<class ELEMENT>
  48. int specialqueue<ELEMENT>::next(int position) {
  49.     return (++position >= this->capacity)? 0: position;
  50. }
  51.  
  52. template<class ELEMENT>
  53. void specialqueue<ELEMENT>::push(const ELEMENT &element) {
  54.     if (this->count >= this->capacity) {
  55.         throw std::runtime_error("ERROR La cola esta llena...");
  56.     }
  57.     this->data[this->tail] = element;
  58.     this->tail = this->next(this->tail);
  59.     ++this->count;
  60. }
  61.  
  62. template<class ELEMENT>
  63. ELEMENT specialqueue<ELEMENT>::front() {
  64.     if (this->count <= 0) {
  65.         throw std::runtime_error("ERROR La cola esta vacía...");
  66.     }
  67.     return this->data[this->head];
  68. }
  69.  
  70. template<class ELEMENT>
  71. void specialqueue<ELEMENT>::pop() {
  72.     if (this->count <= 0) {
  73.         throw std::runtime_error("ERROR La cola esta vacía...");
  74.     }
  75.     this->head = this->next(this->head);
  76.     --this->count;
  77. }
  78.  
  79. template<class ELEMENT>
  80. specialqueue<ELEMENT> specialqueue<ELEMENT>::join(specialqueue<ELEMENT> & other) {
  81.     specialqueue<ELEMENT> result = specialqueue<ELEMENT>((this->size() + other.size()) * 2);
  82.     for(int pos = this->head, c = this->size(); c > 0; --c) {
  83.         result.push(this->data[pos]);
  84.         pos = this->next(pos);
  85.     }
  86.     for(int pos = other.head, c = other.size(); c > 0; --c) {
  87.         result.push(other.data[pos]);
  88.         pos = other.next(pos);
  89.     }
  90.  
  91.     return result;
  92. }
  93.  
  94. #endif //DEMOQUEUE3_SPECIALQUEUE_H
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement