Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include "Queue.h"
- template <class T>
- class QueueArray : public Queue<T>
- {
- public:
- QueueArray(int size = 100); // size задает размер "по умолчанию"
- QueueArray(const QueueArray<T>& src);
- virtual ~QueueArray()
- {
- delete[] array_;
- }
- void enQueue(const T& e);
- const T& deQueue();
- bool isEmpty()
- {
- return head_ == tail_;
- }
- void print(std::ostream& out);
- private:
- T* array_; // массив элементов очереди
- int head_ = 1; // Очередь пуста, если head[Q] = tail[Q].
- int tail_ = 1; // Первоначально: head[Q] = tail[Q] = 1;
- int size_; // размер очереди
- };
- template <class T>
- QueueArray<T>::QueueArray(int size)
- {
- try
- {
- array_ = new T[size_ = size];
- }
- catch (...)
- {
- throw WrongQueueSize();
- }
- head_ = 1;
- tail_ = 1;
- }
- template <class T>
- QueueArray<T>::QueueArray(const QueueArray<T>& src)
- {
- try
- {
- array_ = new T[size_ = src.size_];
- }
- catch (...)
- {
- throw WrongQueueSize();
- }
- }
- template <class T>
- void QueueArray<T>::enQueue(const T& e)
- {
- if (head_ == tail_ + 1)
- {
- throw QueueOverflow();
- }
- tail_ = (tail_ + 1) % size_;
- array_[tail_] = e;
- }
- template <class T>
- const T& QueueArray<T>::deQueue()
- {
- if (head_ == tail_)
- {
- throw QueueUnderflow();
- }
- T copy = array_[head_];
- head_ = (head_ + 1) % size_;
- return copy;
- }
- template <class T>
- void QueueArray<T>::print(std::ostream& out)
- {
- if (head_ == tail_)
- {
- return;
- }
- int count;
- int ix;
- for (ix = (head_ % size_) + 1, count = 1; count <= tail_ - 1; ix = (ix + 1) % size_, count++)
- {
- out << array_[ix] << " ";
- }
- out << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement