From 112a0d344da9e66d20ee9ab5d488e59c5d52e6ad Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Thu, 9 Apr 2020 18:09:42 +0300 Subject: added new examples --- Templates/Queue.hpp | 121 ---------------------------------------------------- 1 file changed, 121 deletions(-) delete mode 100644 Templates/Queue.hpp (limited to 'Templates/Queue.hpp') diff --git a/Templates/Queue.hpp b/Templates/Queue.hpp deleted file mode 100644 index 128f1df..0000000 --- a/Templates/Queue.hpp +++ /dev/null @@ -1,121 +0,0 @@ -#ifndef QUEUE_HDR -#define QUEUE_HDR - -#include -using namespace std; - -template -class Queue { -private: - T * arr; - int capacity; - int count; - - int whereToPut; - int whereToGet; - void Resize(); - void CopyFrom(const Queue& other); - void Free(); - -public: - Queue(); - Queue(const Queue& other); - Queue& operator=(const Queue& other); - ~Queue(); - - bool isEmpty() const; - void Push(T el); - T Pop(); - T& Peek(); -}; - -template -void Queue::Resize() { - T* temp = new T[capacity * 2]; - for (int i = 0; i < capacity; ++i) - temp[i] = Pop(); - whereToGet = 0; - whereToPut = capacity; - count = capacity; - capacity *= 2; - delete[] arr; - arr = temp; -} - -template -void Queue::CopyFrom(const Queue& other) { - arr = new T[other.capacity]; - for (int i = 0; i < other.count; ++i) - arr[i] = other.arr[i]; - count = other.count; - capacity = other.capacity; - whereToPut = other.whereToPut; - whereToGet = other.whereToGet; -} - -template -void Queue::Free() { - delete[] arr; - count = capacity = 0; -} - -template -Queue::Queue() { - arr = new T[4]; - capacity = 4; - count = 0; - whereToPut = 0; - whereToGet = 0; -} - -template -Queue::Queue(const Queue& other) { - CopyFrom(other); -} - -template -Queue& Queue::operator=(const Queue& other) { - if (this != &other) { - Free(); - CopyFrom(other); - } - return *this; -} - -template -Queue::~Queue() { - Free(); -} - -template -bool Queue::isEmpty() const { - return count == 0; -} - -template -void Queue::Push(T el) { - if (count == capacity) - Resize(); - arr[whereToPut] = el; - (whereToPut += 1) %= capacity; - count++; -} - -template -T Queue::Pop() { - if (count == 0) - throw "Queue is empty"; - T elToReturn = arr[whereToGet]; - count--; - (whereToGet += 1) %= capacity; - return elToReturn; -} - -template -T& Queue::Peek() { - if (isEmpty()) - throw std::logic_error("empty queue"); - return arr[whereToGet]; -} - -#endif // !QUEUE_HDR -- cgit v1.3.1-23-gd82d