Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / HomeworkExamPrep / Queue / Queue_Dynamic.hpp
diff options
context:
space:
mode:
Commit9d9bd35065ded1d78c9a07af2953e69fc8eb61d4 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-01-08 22:35:13 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-01-08 22:35:13 +0200
Files16c6371881423dae7690de56e689171e83bb68ec /Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Dynamic.hpp
uni-projects-9d9bd35065ded1d78c9a07af2953e69fc8eb61d4.tar
uni-projects-9d9bd35065ded1d78c9a07af2953e69fc8eb61d4.tar.gz
uni-projects-9d9bd35065ded1d78c9a07af2953e69fc8eb61d4.zip
Parentf3ee1a0ea95ebbd08dd3dcfe341c649d8ce4a8c1 (diff)
[SDP/HEP] Added preparation for "homework" during class
1 files changed, 60 insertions, 0 deletions
-rw-r--r--Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Dynamic.hpp60
diff --git a/Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Dynamic.hpp b/Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Dynamic.hpp
new file mode 100644
index 0000000..02c8888
--- /dev/null
+++ b/Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Dynamic.hpp
@@ -0,0 +1,60 @@
+#ifndef QUEUE_DYNAMIC
+#define QUEUE_DYNAMIC
+
+template <class T>
+class Queue {
+ struct Node {
+ T val;
+ Node* next;
+
+ Node(const T& v) : val(v), next(nullptr) {}
+ };
+
+ Node* head;
+ Node* tail;
+
+public:
+ Queue();
+ ~Queue();
+ Queue(Queue& other) = delete;
+ Queue& operator=(Queue& other) = delete;
+
+ void push(const T& v);
+ void pop();
+ T& front();
+};
+
+template <class T>
+Queue<T>::Queue() : head(nullptr), tail(nullptr) {}
+
+template <class T>
+Queue<T>::~Queue() {
+ while (head != nullptr)
+ pop();
+}
+
+template <class T>
+void Queue<T>::push(const T& v) {
+ if (head == nullptr) {
+ head = tail = new Node(v);
+ }
+ else {
+ tail->next = new Node(v);
+ tail = tail->next;
+ }
+}
+
+template <class T>
+void Queue<T>::pop() {
+ Node* toDel = head;
+ head = head->next;
+ delete toDel;
+ if (head == nullptr) tail = nullptr;
+}
+
+template <class T>
+T& Queue<T>::front() {
+ return head->val;
+}
+
+#endif