Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. //
  2. //  QueueArrayСyclic.hpp
  3. //  Queue_Trees
  4. //
  5. //  Created by zhozh on 28.03.2020.
  6. //  Copyright © 2020 zhozh. All rights reserved.
  7. //
  8.  
  9. #ifndef QueueArrayCyclic_hpp
  10. #define QueueArrayCyclic_hpp
  11.  
  12. #include <stdio.h>
  13. #include <iostream>
  14. #include "Queue.hpp"
  15. #include "QueueExceptions.hpp"
  16.  
  17. using std::cout;
  18.  
  19. //==============================================================
  20. // Шаблон класса QueueArray - реализация круговой очереди.
  21. // Элементы очереди помещаются в массив.
  22. //==============================================================
  23.  
  24. template <class T>
  25. class QueueArray: public Queue<T>
  26. {
  27. public:
  28.     QueueArray(int size = 100); // size задает размер "по умолчанию"
  29.  
  30.     QueueArray(const QueueArray<T>& src);
  31.  
  32.     virtual ~QueueArray() { delete[] array_; }
  33.  
  34.     void enQueue(const T& element) override;
  35.  
  36.     const T& deQueue() override;
  37.  
  38.     bool isEmpty() const override { return head_ == tail_; }
  39.    
  40.     T& peekFront() const { return array_[head_]; }
  41.  
  42.     void show() const;
  43. private:
  44.     T* array_; // массив элементов очереди
  45.     int head_ = 1; // Очередь пуста, если head[Q] = tail[Q].
  46.     int tail_ = 1; // Первоначально: head[Q] = tail[Q] = 1;
  47.     int size_; // размер очереди
  48. };
  49.  
  50. template <class T>
  51. QueueArray<T>::QueueArray(int size):
  52.     size_(size)
  53. {
  54.     try
  55.     {
  56.         array_ = new T[size_ + 1]; // пытаемся заказать память под элементы стека...
  57.     }
  58.     catch (...)
  59.     {                              // если что-то случилось (например, размер слишком большой
  60.         throw WrongQueueSize();    // или отрицательный) - возникает искл.ситуация
  61.     }
  62. }
  63.  
  64. template <class T>
  65. QueueArray<T>::QueueArray(const QueueArray<T>& src):
  66.     head_(src.head_),
  67.     tail_(src.tail_),
  68.     size_(src.size_)
  69. {
  70.     try
  71.     {
  72.         array_ = new T[size_ + 1];
  73.     }
  74.     catch (...)
  75.     {
  76.         throw WrongQueueSize();
  77.     }
  78.    
  79.     size_t i = head_;
  80.    
  81.     do
  82.     {
  83.         array_[i] = src.array_[i];
  84.        
  85.         if (i == size_)
  86.         {
  87.             i = 0;
  88.         }
  89.         else
  90.         {
  91.             i++;
  92.         }
  93.            
  94.     } while (i != tail_);
  95. }
  96.  
  97. template <class T>
  98. void QueueArray<T>::enQueue(const T& element)
  99. {
  100.     if (head_ == tail_ + 1)
  101.     {
  102.         throw QueueOverflow(); // нет места для нового элемента
  103.     }
  104.    
  105.     array_[tail_] = element;
  106.    
  107.     if (tail_ == size_)
  108.     {
  109.         tail_ = 0;
  110.     }
  111.     else
  112.     {
  113.         tail_++;
  114.     }
  115. }
  116.  
  117. template <class T>
  118. const T& QueueArray<T>::deQueue()
  119. {
  120.     if (head_ == tail_)
  121.     {
  122.         throw QueueUnderflow(); // очередь пуста
  123.     }
  124.    
  125.     const T& data = array_[head_];
  126.    
  127.     if (head_ == size_)
  128.     {
  129.         head_ = 0;
  130.     }
  131.     else
  132.     {
  133.         head_++;
  134.     }
  135.  
  136.     return data;
  137. }
  138.  
  139. template<class T>
  140. inline void QueueArray<T>::show() const
  141. {
  142.     if (this->isEmpty())
  143.     {
  144.         cout << " is empty";
  145.     }
  146.     else
  147.     {
  148.         size_t i = head_;
  149.         do
  150.         {
  151.             cout << array_[i] << ' ';
  152.             if (i == size_)
  153.             {
  154.                 i = 0;
  155.             }
  156.             else
  157.             {
  158.                 i++;
  159.             }
  160.            
  161.         } while (i != tail_);
  162.     }
  163. }
  164.  
  165. #endif /* QueueArrayCyclic_hpp */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement