Website Summary Files Commits Branches @commit @diff @stats
path: root / Templates / Queue / Queue.hpp
diff options
context:
space:
mode:
Commit112a0d344da9e66d20ee9ab5d488e59c5d52e6ad (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Filese3bb9cf17c24a3b53c746d9142906d550d549465 /Templates/Queue/Queue.hpp
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar.gz
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.zip
Parentec26fab32d144b436d08f9da6049bd053156d783 (diff)
added new examples
1 files changed, 121 insertions, 0 deletions
-rw-r--r--Templates/Queue/Queue.hpp121
diff --git a/Templates/Queue/Queue.hpp b/Templates/Queue/Queue.hpp
new file mode 100644
index 0000000..128f1df
--- /dev/null
+++ b/Templates/Queue/Queue.hpp
@@ -0,0 +1,121 @@
+#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