Brumski's DSA Learning Project 1.3
For Learning Data Structures and Algorithms in C++
Loading...
Searching...
No Matches
queue_dsa.hpp
1#pragma once
2#include <memory>
3#include <iostream>
4
10template<class T>
11class MyQueue {
12private:
16 template<class NType>
17 class Node {
18 public:
20 std::shared_ptr<Node<NType>> next = nullptr;
25 Node() = default;
26 };
27private:
28 std::shared_ptr<Node<T>> theFirst;
29 std::shared_ptr<Node<T>> theRear;
30public:
34 MyQueue() = default;
35
39 MyQueue(const MyQueue<T>&) = delete;
40
45 MyQueue(MyQueue<T>&& other) = default;
46
50 MyQueue& operator=(const MyQueue<T>&) = delete;
51
56 MyQueue& operator=(MyQueue<T>&& other) = default;
57public:
62 virtual MyQueue<T>& push_back(const T& value) {
63 std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>();
64 newNode->data = value;
65 newNode->next = nullptr;
66
67 if (!theFirst) {
68 theFirst = theRear = newNode;
69 return *this;
70 }
71 else {
72 theRear->next = newNode;
73 theRear = newNode;
74 return *this;
75 }
76
77 return *this;
78 }
79
83 virtual T front_element() {
84 return theFirst->data;
85 }
86
90 virtual T last_element() {
91 Node<T>* temp = theFirst.get();
92 while (temp->next != nullptr) {
93 temp = temp->next.get();
94 }
95
96 return temp->data;
97 }
98
102 virtual T popFront() {
103 if (theFirst == nullptr) throw std::runtime_error("cannot dequeue empty queue");
104 T data = theFirst->data;
105 theFirst = theFirst->next;
106 if (theFirst == nullptr) theRear = nullptr;
107
108 return data;
109 }
110
114 virtual bool isEmpty() {
115 if (theFirst) return false;
116 else return true;
117
118 return false;
119 }
120
121 virtual size_t size() {
122 Node<T>* temp = theFirst.get();
123 size_t counter = 0;
124 while (temp) {
125 temp = temp->next.get();
126 counter++;
127 }
128
129 return counter;
130 }
131};
132
136void UsingQueue();
137
141void UsingQueue2();
142
147template<class T>
148void UsingQueue3(MyQueue<T>& deez) {
149 while (!deez.isEmpty()) {
150 std::cout << "The first in the queue right now is " << deez.front_element() << std::endl;
151 deez.popFront();
152 std::cout << "The first has been popped!" << std::endl;
153 }
154}
This is a template class that represents a single element of a queue, and it will be called "Node".
Definition queue_dsa.hpp:17
std::shared_ptr< Node< NType > > next
Stores the address of the next node.
Definition queue_dsa.hpp:20
NType data
Stores the data for each node.
Definition queue_dsa.hpp:19
Node()=default
Default Constructor.
This is a template class that represents the queue data structure.
Definition queue_dsa.hpp:11
virtual T last_element()
Prints out the last element in the queue.
Definition queue_dsa.hpp:90
virtual T popFront()
Removes the first element in the queue.
Definition queue_dsa.hpp:102
MyQueue(const MyQueue< T > &)=delete
Deleted copy constructor.
virtual T front_element()
Prints out the front element in the queue.
Definition queue_dsa.hpp:83
std::shared_ptr< Node< T > > theRear
Pointer to the last element of the queue.
Definition queue_dsa.hpp:29
virtual MyQueue< T > & push_back(const T &value)
Inserts a value at the end of the queue.
Definition queue_dsa.hpp:62
MyQueue & operator=(const MyQueue< T > &)=delete
Deleted copy assignment operator.
MyQueue & operator=(MyQueue< T > &&other)=default
Default Move assignment operator.
std::shared_ptr< Node< T > > theFirst
Pointer to the first element of the queue.
Definition queue_dsa.hpp:28
MyQueue()=default
Default Constructor.
virtual bool isEmpty()
Returns true if the queue is empty.
Definition queue_dsa.hpp:114
MyQueue(MyQueue< T > &&other)=default
Default Move constructor.