-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
195 lines (144 loc) · 4.14 KB
/
Queue.h
File metadata and controls
195 lines (144 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Author: Jerome Richards
// Queue
/*---------------------------------------------------------------
| The class is a replication of the queue class in C++ and is a |
| demonstration of employing my logic to recreate it. |
---------------------------------------------------------------*/
#include <iostream>
template <class type>
class Queue {
private:
Queue* head;
Queue* next;
Queue* end;
type* content; // The actual content of the element
int length;
public:
Queue();
void Push(type&); // Push element if paramter is lvalue
void Push(type&&); // Push element if paramter is rvalue
void Pop(); // Pops element from front of queue
void Traverse(); // Lists all elements in the stack
type& getElementAt(int); // Returns element at index
int getLength(); // Returns the number of elements in queue
};
/*-----------------------------------------------------
| The constructor initializes all pointers to nullptr |
-----------------------------------------------------*/
template <class type>
Queue<type>::Queue()
{
head = nullptr;
next = nullptr;
end = nullptr;
content = nullptr;
length = 0;
}
/*------------------------------------------------------------------------
| Receives an lvalue parameter and adds element to the back of the queue |
------------------------------------------------------------------------*/
template <class type>
void Queue<type>::Push(type& q)
{
if (head == nullptr)
{
head = new Queue();
head->content = &q;
end = head;
}
else
{
end->next = new Queue();
end = end->next;
end->content = &q;
}
length++;
}
/*------------------------------------------------------------------------
| Receives an rvalue parameter and adds element to the back of the queue |
------------------------------------------------------------------------*/
template <class type>
void Queue<type>::Push(type&& q)
{
if (head == nullptr)
{
head = new Queue();
head->content = new type;
*head->content = q;
end = head;
}
else
{
end->next = new Queue();
end = end->next;
end->content = new type;
*end->content = q;
}
length++;
}
/*-------------------------------------------
| Removes the element in front of the queue |
-------------------------------------------*/
template <class type>
void Queue<type>::Pop()
{
if (head != nullptr)
{
Queue* temp = head;
head = head->next;
delete temp;
length--;
}
else
{
std::cout << "The Queue is already empty" << std::endl;
}
}
/*-------------------------------------------------------------------------------
| Lists all elements in the queue starting from the front and going to the back |
-------------------------------------------------------------------------------*/
/*--- IMPORTANT ---*/
//
// This particular transverse function assumes the content is a primitive data type - int, float, double, string, etc.,
// and can print the content without issue. However, if the stack contains class objects or structs then the function
// will need to be modified in order to print out the desired attributes of the content.
//
template<class type>
void Queue<type>::Traverse()
{
if (head != nullptr)
{
Queue* temp = head;
while (temp != end)
{
std::cout << *temp->content << std::endl;
temp = temp->next;
}
std::cout << *temp->content << std::endl;
}
else
{
std::cout << "The Queue is empty" << std::endl;
}
}
/*--------------------------------------------------------------------------------
| Returns element at the specified index where the front of the queue is index 0 |
--------------------------------------------------------------------------------*/
template <class type>
type& Queue<type>::getElementAt(int n)
{
Queue* temp = head;
for (int i = 0; i < n; i++)
{
temp = temp->next;
}
return *temp->content;
}
/*---------------------------------------------
| Returns the number of elements in the queue |
---------------------------------------------*/
template <class type>
int Queue<type>::getLength()
{
return length;
}