Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit5350b9f4138e70cb00f72999336437e995798244 (patch)
Author
 Angeld55 <achi_d@abv.bg>
2019-09-20 15:02:58 +0300
Committer
 GitHub <noreply@github.com>
2019-09-20 15:02:58 +0300
Files052f8f25f9d467d21692d3e891b11eb58746a177
Object-oriented_programming_FMI-5350b9f4138e70cb00f72999336437e995798244.tar
Object-oriented_programming_FMI-5350b9f4138e70cb00f72999336437e995798244.tar.gz
Object-oriented_programming_FMI-5350b9f4138e70cb00f72999336437e995798244.zip
Parent84ae5d6e9df08cfc793e68c7066d670c367a2130 (diff)
Update Queue.hpp
1 files changed, 121 insertions, 1 deletions
-rw-r--r--Templates/Queue.hpp122
diff --git a/Templates/Queue.hpp b/Templates/Queue.hpp
index 4c559f7..1f5cc18 100644
--- a/Templates/Queue.hpp
+++ b/Templates/Queue.hpp
@@ -1 +1,121 @@
-j
+ifndef QUEUE_HDR
+#define QUEUE_HDR
+
+#include <iostream>
+using namespace std;
+
+template<typename T>
+class Queue {
+private:
+ T * arr;
+ int capacity;
+ int count;
+
+ int whereToPut;
+ int whereToGet;
+ void Resize();
+ void CopyFrom(const Queue<T>& other);
+ void Free();
+
+public:
+ Queue();
+ Queue(const Queue<T>& other);
+ Queue<T>& operator=(const Queue<T>& other);
+ ~Queue();
+
+ bool isEmpty() const;
+ void Push(T el);
+ T Pop();
+ T& Peek();
+};
+
+template <typename T>
+void Queue<T>::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 <typename T>
+void Queue<T>::CopyFrom(const Queue<T>& 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 <typename T>
+void Queue<T>::Free() {
+ delete[] arr;
+ count = capacity = 0;
+}
+
+template <typename T>
+Queue<T>::Queue() {
+ arr = new T[4];
+ capacity = 4;
+ count = 0;
+ whereToPut = 0;
+ whereToGet = 0;
+}
+
+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();
+}
+
+template <typename T>
+bool Queue<T>::isEmpty() const {
+ return count == 0;
+}
+
+template <typename T>
+void Queue<T>::Push(T el) {
+ if (count == capacity)
+ Resize();
+ arr[whereToPut] = el;
+ (whereToPut += 1) %= capacity;
+ count++;
+}
+
+template <typename T>
+T Queue<T>::Pop() {
+ if (count == 0)
+ throw "Queue is empty";
+ T elToReturn = arr[whereToGet];
+ count--;
+ (whereToGet += 1) %= capacity;
+ return elToReturn;
+}
+
+template <typename T>
+T& Queue<T>::Peek() {
+ if (isEmpty())
+ throw std::logic_error("empty queue");
+ return arr[whereToGet];
+}
+
+#endif // !QUEUE_HDR