Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 09 / task2 / Queue.hpp
diff options
context:
space:
mode:
Commitbef155afbaab2bf9b9da5253a7eea45dbb700316 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-05-05 02:25:55 +0300
Committer
 GitHub <noreply@github.com>
2021-05-05 02:25:55 +0300
Filesdeff0975a44534d1a0f41cdb5ac3a1f3f73e1969 /Sem. 09/task2/Queue.hpp
Object-oriented_programming_FMI-bef155afbaab2bf9b9da5253a7eea45dbb700316.tar
Object-oriented_programming_FMI-bef155afbaab2bf9b9da5253a7eea45dbb700316.tar.gz
Object-oriented_programming_FMI-bef155afbaab2bf9b9da5253a7eea45dbb700316.zip
Parent45c11bbae0d85f64538e8a392d18cf93196a2257 (diff)
Added big 4 for queue
1 files changed, 60 insertions, 7 deletions
-rw-r--r--Sem. 09/task2/Queue.hpp67
diff --git a/Sem. 09/task2/Queue.hpp b/Sem. 09/task2/Queue.hpp
index 1cc14c3..1f7f854 100644
--- a/Sem. 09/task2/Queue.hpp
+++ b/Sem. 09/task2/Queue.hpp
@@ -6,19 +6,27 @@ 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<T>& other);
+ void free();
public:
Queue();
+ Queue(const Queue<T>& other);
+ Queue<T>& operator=(const Queue<T>& other);
+
void enqueue(const T& obj);
T dequeue();
size_t getCount();
size_t isEmpty();
+
+ ~Queue();
};
template <typename T>
@@ -40,9 +48,9 @@ void Queue<T>::enqueue(const T& obj)
resize(capacity * 2);
if (count != 0)
tail++;
-
+
tail %= capacity; //so we don't get an out of range exception
-
+
data[tail] = obj;
count++;
@@ -53,10 +61,10 @@ T Queue<T>::dequeue()
{
if (count == 0)
throw std::exception("The collection is empty!");
-
+
T toReturn = data[head];
(++head) %= capacity;
-
+
count--;
return toReturn;
@@ -81,11 +89,56 @@ void Queue<T>::resize(size_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;
-} \ No newline at end of file
+}
+
+template <typename T>
+void Queue<T>::copyFrom(const Queue<T>& 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 <typename T>
+void Queue<T>::free()
+{
+ delete[] data;
+}
+
+template <typename T>
+Queue<T>::Queue(const Queue<T>& other)
+{
+ copyFrom(other);
+
+}
+
+template <typename T>
+Queue<T>& Queue<T>::operator=(const Queue<T>& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <typename T>
+Queue<T>::~Queue()
+{
+ free();
+}