From 42f59920cf5d300a54f3dae03911dccbd1a86eea Mon Sep 17 00:00:00 2001 From: angeld55 Date: Wed, 12 May 2021 19:47:03 +0300 Subject: sem 11 + split sem 10 --- Sem. 10/task1/Queue.hpp | 144 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Sem. 10/task1/Queue.hpp (limited to 'Sem. 10/task1/Queue.hpp') diff --git a/Sem. 10/task1/Queue.hpp b/Sem. 10/task1/Queue.hpp new file mode 100644 index 0000000..1f7f854 --- /dev/null +++ b/Sem. 10/task1/Queue.hpp @@ -0,0 +1,144 @@ +#include + +template +class Queue +{ + T* data; + size_t capacity; + size_t count; + + size_t head; //where to get + size_t tail; //last added element; + + void resize(size_t newCapacity); + + void copyFrom(const Queue& other); + void free(); +public: + Queue(); + + Queue(const Queue& other); + Queue& operator=(const Queue& other); + + void enqueue(const T& obj); + T dequeue(); + + size_t getCount(); + size_t isEmpty(); + + ~Queue(); +}; + +template +Queue::Queue() +{ + capacity = 4; + data = new T[capacity]; + + count = 0; + head = tail = 0; +} + + + +template +void Queue::enqueue(const T& obj) +{ + if (count == capacity) + resize(capacity * 2); + if (count != 0) + tail++; + + tail %= capacity; //so we don't get an out of range exception + + data[tail] = obj; + + count++; +} + +template +T Queue::dequeue() +{ + if (count == 0) + throw std::exception("The collection is empty!"); + + T toReturn = data[head]; + (++head) %= capacity; + + count--; + + return toReturn; +} + +template +size_t Queue::getCount() +{ + return count; +} + +template +size_t Queue::isEmpty() +{ + return getCount() == 0; +} + +template +void Queue::resize(size_t newCapacity) +{ + T* newData = new T[newCapacity]; + + for (int i = 0, ind = head; i < count; i++, (++ind) %= capacity) + newData[i] = data[ind]; + + delete[] data; + capacity = newCapacity; + data = newData; + + head = 0; + tail = count - 1; +} + +template +void Queue::copyFrom(const Queue& other) +{ + data = new T[other.capacity]; + for (int i = 0; i < other.capacity; i++) + data[i] = other.data[i]; + + head = other.head; + tail = other.tail; + + count = other.count; + capacity = other.capacity; + +} + +template +void Queue::free() +{ + delete[] data; +} + +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(); +} -- cgit v1.3.1-23-gd82d