Repository navigation
Expand file tree
/
Copy pathLqueue.h
More file actions
341 lines (314 loc) · 12.8 KB
/
Copy pathLqueue.h
File metadata and controls
341 lines (314 loc) · 12.8 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
template <class T>
class Node {
public:
Node( const T& data ); //Node Constructor
T& GetData(); //Node Data Getter
void SetData( const T& data ); //Node Data Setter
Node<T>* GetNext(); //Node m_next getter
void SetNext( Node<T>* next ); //Node m_next setter
private:
T m_data; //Holds templated data
Node<T>* m_next; //Pointer to next node
};
template <class T>
Node<T>::Node( const T& data ) {
m_data = data;
m_next = nullptr;
}
template <class T>
T& Node<T>::GetData() {
return m_data;
}
template <class T>
void Node<T>::SetData( const T& data ) {
m_data = data;
}
template <class T>
Node<T>* Node<T>::GetNext() {
return m_next;
}
template <class T>
void Node<T>::SetNext( Node<T>* next ) {
m_next = next;
}
template <class T>
class Lqueue {
public:
// Name: Lqueue() (Linked List Queue) - Default Constructor
// Desc: Used to build a new linked queue (as a linked list)
// Preconditions: None
// Postconditions: Creates a new lqueue where m_head and m_tail point to nullptr
Lqueue();
// Name: ~Lqueue() - Destructor
// Desc: Used to destruct a Lqueue
// Preconditions: There is an existing lqueue with at least one node
// Postconditions: An lqueue is deallocated (including dynamically allocated nodes)
// to have no memory leaks!
~Lqueue();
// Name: Lqueue (Copy Constructor)
// Preconditions: Creates a copy of existing LQueue
// Requires a Lqueue
// Postconditions: Copy of existing Lqueue
Lqueue(const Lqueue&);
// Name: operator= (Overloaded Assignment Operator)
// Preconditions: Copies an Lqueue into an existing Lqueue
// Requires a Lqueue
// Postconditions: Copy of existing Lqueue
Lqueue<T>& operator= (const Lqueue&);
// Name: Push
// Preconditions: Takes in data. Creates new node.
// Requires a Lqueue
// Postconditions: If the bool = true, adds to front else adds a new node to the
// end of the lqueue.
void Push(const T&, bool);
// Name: Pop
// Preconditions: Lqueue with at least one node.
// Postconditions: Removes first node in the lqueue, returns data from first node.
T Pop();
// Name: Display
// Preconditions: Outputs the lqueue.
// Postconditions: Displays the data in each node of lqueue
void Display();
// Name: Front
// Preconditions: Requires a populated lqueue
// Postconditions: Returns whatever data is in front
T Front();
// Name: IsEmpty
// Preconditions: Requires a lqueue
// Postconditions: Returns if the lqueue is empty.
bool IsEmpty();
// Name: GetSize
// Preconditions: Requires a lqueue
// Postconditions: Returns m_size
int GetSize();
// Name: Swap(int)
// Preconditions: Requires a lqueue
// Postconditions: Swaps the nodes at the index with the node prior to it.
void Swap(int);
// Name: Clear
// Preconditions: Requires a lqueue
// Postconditions: Removes all nodes in a lqueue
void Clear();
// Name: TestHeadTail() (Optional test function)
// Preconditions: Requires a lqueue
// Postconditions: Returns the value of the head and the tail (used for main below)
void TestHeadTail();
// Name: operator<< (Overloaded << operator)
// Preconditions: Requires a lqueue
// Postconditions: Returns an ostream with the data from each node on different line
template <class U>
friend ostream& operator<<(ostream& output, const Lqueue<U>&);
//Name: Overloaded [] operator
//Precondition: Existing Lqueue
//Postcondition: Returns object from Lqueue using []
T& operator[] (int x);//Overloaded [] operator to pull data from Lqueue
private:
Node <T> *m_head; //Node pointer for the head
Node <T> *m_tail; //Node pointer for the tail
int m_size = 0; //Number of nodes in queue
};
//*****************************************************************
//Implement Lqueue here
// Name: Lqueue() (Linked List Queue) - Default Constructor
template <class T>
Lqueue<T>::Lqueue(){
m_head = nullptr;
m_tail = nullptr;
}
// Name: ~Lqueue() - Destructor
template <class T>
Lqueue<T>::~Lqueue(){
Node<T> *curr = m_head;
Node<T> *temp = curr;
while(temp != NULL){
curr = temp->GetNext();
delete temp;
temp = curr;
m_size--;
}
delete curr;
}
// Name: Lqueue (Copy Constructor)
template <class T>
Lqueue<T>::Lqueue(const Lqueue& queue){
m_head = nullptr;
m_tail = nullptr;
for(Node<T> *cur = queue.m_head;cur!=NULL;cur=cur->GetNext()){
Push(cur->GetData(),true);
}
}
// Name: operator= (Overloaded Assignment Operator)
template <class T>
Lqueue<T>&Lqueue<T>:: operator= (const Lqueue& queue){
Clear();
m_head = nullptr;
m_tail = nullptr;
for(Node<T> *cur = queue.m_head;cur!=NULL;cur=cur->GetNext()){
Push(cur->GetData(),true);
}
return *this;
}
// Name: Push
// Preconditions: Takes in data. Creates new node.
// Requires a Lqueue
// Postconditions: If the bool = true, adds to front else adds a new node @ the
// end of the lqueue.
template <class T>
void Lqueue<T>::Push(const T& data1, bool booly){
//inserting at the end
if(booly == true){
// cout << "in the end" << endl;
Node<T> *temp = new Node<T>(data1);
temp->SetNext(nullptr);
if (m_head == nullptr){
m_head = temp;
m_tail = temp;
temp->SetNext(nullptr);
}else{
m_tail->SetNext(temp);
m_tail = temp;
}
}
//inserting at the beginning
else{
// cout << "in the beginning" << endl;
Node<T> *temp = new Node<T>(data1);
if(m_head == nullptr){
m_head = temp;
temp->SetNext(nullptr);
}
else{
Node<T> *ptr;
ptr = m_head;
temp->SetNext(ptr);
m_head = temp;
}
}
m_size++;
}
// Name: Pop
template <class T>
T Lqueue<T>::Pop(){
m_size--;
Node<T> *front = m_head->GetNext();
delete m_head;
m_head = NULL;
m_head = front;
T popped = front->GetData();
return popped;
}
// Name: Display
template <class T>
void Lqueue<T>::Display(){
if(IsEmpty() == true)
cout << "" << endl;
else{
Node<T> *curr = m_head;
for(int numNodes = 0; numNodes< m_size; numNodes++){
cout << curr->GetData()<< " -> ";
curr = curr->GetNext();
}cout << "END" << endl;
}
}
// Name: Front
template <class T>
T Lqueue<T>::Front(){
T front = m_head->GetData();
return front;
}
// Name: IsEmpty
template <class T>
bool Lqueue<T>::IsEmpty(){
if(m_size == 0){
cout << "The linked list is empty" << endl;
return true;
}else{
return false;
}
}
// Name: GetSize
template <class T>
int Lqueue<T>::GetSize(){
return m_size;
}
// Name: Swap(int)
template <class T>
void Lqueue<T>::Swap(int swapper){
if(IsEmpty() == true or GetSize() == 1)
cout << "This is empty or has only 1 item. Nothing to swap" << endl;
else if(swapper <= 0){
cout << "no node before the 0th node. cannot swap";
}
else if(swapper == 1){
Node<T> *prev = (m_head);
Node<T> *curr = (prev->GetNext());
prev->SetNext(curr->GetNext());
curr->SetNext(prev);
m_head = curr;
}
else if(swapper < GetSize()){
Node<T> *prev2 = (m_head);
Node<T> *prev1 = (prev2->GetNext());
Node<T> *curr = (prev1->GetNext());
int count = 2;
while(swapper > count){
prev2 = prev2->GetNext();
prev1 = prev1->GetNext();
curr = curr->GetNext();
count++;
}
prev2->SetNext(curr);
prev1->SetNext(curr->GetNext());
curr->SetNext(prev1);
if(swapper+1 == GetSize())
m_tail = prev1;
}
}
// Name: Clear
template <class T>
void Lqueue<T>::Clear(){
if (IsEmpty() == false){
Node<T> *curr = m_head;
while(m_head != NULL){
curr = m_head->GetNext();
curr = NULL;
m_head = curr;
}
}
m_size = 0;
}
// Name: TestHeadTail() (Optional test function)
template <class T>
void Lqueue<T>::TestHeadTail(){
cout << m_head->GetData() << " " << m_tail->GetData() << endl;
}
// Name: operator<< (Overloaded << operator)
template <class U>
ostream& operator<<(ostream& output, const Lqueue<U>& queue){
if(queue.m_head){
Node<U> *temp = queue.m_head;
while(temp != NULL){
output << temp->GetData() << " -> ";
temp = temp->GetNext();
}output << "END" << endl;
}else
output << "This is empty" << endl;
return output;
}
//Name: Overloaded [] operator
template <class T>
T& Lqueue<T>::operator[] (int x){
Node<T> *curr = m_head;
T now1 = curr->GetData();
for(int i = 0; i < GetSize(); i++){
if(i == x)
return curr->GetData();
curr = curr->GetNext();
}
return curr->GetData();
}