-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
465 lines (414 loc) · 12.9 KB
/
Copy pathVector.cpp
File metadata and controls
465 lines (414 loc) · 12.9 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//
// Vector.cpp
// Project 5 - Templates
//
// Created by Amarjot Gill 4/26/2021
// creates a Vector which is a linked list, can do various things with another vector and it's self.
#ifndef VECTOR_CPP
#define VECTOR_CPP
#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;
const int ONE = 1;
const int TWO = 2;
const char TRUE = 'T';
const char FALSE = 'F';
template <class T>
struct Node {
public:
Node(T value) {
m_value = value;
next = nullptr;
}
Node(T value, Node* _next) {
m_value = value;
next = _next;
}
T getValue() {
return m_value;
}
Node<T>* getNextNode() {
return next;
}
void setNextNode(Node<T>* newNode) {
next = newNode;
}
private:
T m_value;
Node<T>* next;
};
template <class T>
class Vector {
public:
// Name: Default Constructor
// Precondition: None (Must be templated)
// Postcondition: Creates a vector using a linked list
Vector();
// Name: Destructor
// Desc: Frees memory
// Precondition: Existing Vector
// Postcondition: Destructs existing Vector
~Vector();
// Name: Copy Constructor
// Desc: Copies an existing vector
// Precondition: Existing Vector
// Postcondition: Two identical vectors (in separate memory spaces)
// Hint: Utilize overloaded [] operator
Vector (Vector<T> & source);
// Name: Overloaded Assignment operator
// Desc: Assingns a vector
// Precondition: Existing Vector
// Postcondition: Assigns a vector
// Hint: Utilize overloaded [] operator
Vector<T>* operator=(Vector<T>* source);
// Name: Overloaded [] operator
// Desc: to retrive use [indx]
// Precondition: Existing Vector
// Postcondition: Returns the value of the element at the given index
T operator[](int indx);
// Name: Insert
// Desc: insert a node to the end of the vector
// Precondition: Existing Vector
// Postcondition: A vector with the newly added value
void Insert(T);
// Name: SortedInsert
// Desc: Inserts a node into the vector at it's correct position (sorted ascendingly)
// Precondition: Existing Vector
// Postcondition: sorted vector (low to high)
void SortedInsert(T);
// Name: Remove
// Desc: removes a node from the vector
// Precondition: Existing Vector
// Postcondition: A vector that holds the results of the vectors added
void Remove(int indx);
// Name: Overloaded + operator | Vector Addition
// Desc: Adds two vectors and returns the result
// Precondition: Existing Vector, vectors can be of different size
// Postcondition: A vector that holds the results of the vectors added
Vector<T>* operator+(Vector<T>& source);
// Name: Overloaded * operator | Vector Multiplication
// Desc: Multiplys two vectors and returns the result
// Precondition: Existing Vector, vectors can be of different size
// Postcondition: returns a vector that holds the results of the vectors multiplied
Vector<T>* operator*(Vector<T>& other);
// Name: Overloaded < operator | Vector Comparision
// Desc: Compares two vectors [using the < operator] and returns the result
// Precondition: Existing Vector -> vectors need to be of the same size
// Postcondition: returns a vector that holds the boolean char (T or F) value of each node comparison
Vector<char>* operator<(Vector<T>& other);
// Name: Overloaded == operator | Vector Comparision
// Desc: Compares two vectors [using the == operator] and returns the result
// Precondition: Existing Vector, vectors need to be of the same size
// Postcondition: returns a vector that holds the boolean char (T or F) value of each node comparison
Vector<char>* operator==(Vector<T>& other);
// Name: Size
// Desc: number of nodes in Vector
// Precondition: Existing Vector
// Postcondition: returns the size of the vector
int Size();
// Name: Display
// Desc: displays the contents of the vector
// Precondition: Existing Vector
// Postcondition: prints to console the contents of Vector
void Display();
// Name: median
// Desc: Computes the median of the vector
// Precondition: Existing Vector
// Postcondition: returns the median value
float Median();
// Name: Mean
// Desc: Computes the mean of the vector
// Precondition: Existing Vector
// Postcondition: returns the mean value
float Mean();
// Name: StDev
// Desc: Computes the standard derivation of the vector
// Precondition: Existing Vector
// Postcondition: returns the standard derivation
float StDev();
private:
Node<T>* m_head;
};
// **** Add class definition below ****
template <class T>
Vector<T>::Vector(){
m_head = nullptr;
}
template <class T>
Vector<T>::Vector(Vector<T> & source){
int i = 0;
// if source m_head is null then Vector is empty
if (source.m_head == nullptr)
return;
Node<T>* tmp = source.m_head;
// allocates m_head as same as source
m_head = new Node<T>(source.m_head -> getValue());
// current starts at m_head
Node<T>* current = m_head;
tmp = tmp-> getNextNode();
while (tmp != nullptr){
// Allocates new node with same value as source
Node<T>* newNode = new Node<T>(source.operator[](i));
i++;
// Point to this new node.
current-> setNextNode(newNode);
current = current -> getNextNode();
current -> setNextNode(nullptr);
tmp = tmp-> getNextNode();
}
}
template <class T>
Vector<T>* Vector<T>::operator=(Vector<T>* source){
// = operator does exact same as copy makes deep copy
int i = 0;
if (source -> m_head == nullptr)
return *this;
Node<T>* tmp = source -> m_head;
m_head = new Node<T>(tmp -> getValue());
m_head -> setNextNode(nullptr);
Node<T>* current = m_head;
tmp = tmp->getNextNode();
while (tmp != nullptr){
// since deep copy making new nodes completely
Node<T>* newNode = new Node<T>(source->operator[](i));
i++;
current-> setNextNode(newNode);
current = current -> getNextNode();
// set the 'next' to null
current -> setNextNode(nullptr);
tmp = tmp-> getNextNode();
}
cout << &source -> m_head << endl;
cout << &m_head << endl;
return *this;
}
template <class T>
void Vector<T>::Insert(T nodeData){
// makes new node
Node<T> *newNode = new Node<T>(nodeData);
if (m_head == nullptr){
m_head = newNode;
}else{
Node<T> *temp = m_head;
// traverses to end of list
while (temp -> getNextNode() != nullptr){
temp = temp -> getNextNode();
}
// sets end of list = to newNode
temp -> setNextNode(newNode);
}
}
template <class T>
void Vector<T>::SortedInsert(T nodeData){
Node<T>* newNode = new Node<T>(nodeData);
/* Special case for the head end */
if (m_head == nullptr|| m_head-> getValue() >= newNode -> getValue()){
newNode -> setNextNode(m_head);
m_head = newNode;
}
else{
Node<T>* current = m_head;
// will run will current isnt at end and currents value is smaller then newNodes
while (current-> getNextNode() != nullptr && current -> getNextNode()-> getValue() < newNode -> getValue()){
current = current -> getNextNode();
}
// once the spot is found newNodes next is currents next
newNode -> setNextNode(current -> getNextNode());
// currents new next is newNode
current-> setNextNode(newNode);
}
}
template <class T>
int Vector<T>::Size(){
int size = 0;
Node<T> *temp = m_head;
// traverses linked list and updates size as it goes
while (temp != nullptr){
size++;
temp = temp -> getNextNode();
}
temp = nullptr;
return size;
}
template <class T>
T Vector<T>::operator[](int indx){
Node<T> *temp = m_head;
for (int i = 0; i < Size(); i++){
// once i = the indx it returns temps value
if (i == indx){
return temp -> getValue();
// else temp will go to the next node
}else{
temp = temp -> getNextNode();
}
}
return temp -> getValue();
}
template <class T>
void Vector<T>::Display(){
if (Size() == 0){
cout << "Vector is empty" << endl;
return;
}
else{
// traverses linked list and displays each value in it
Node<T> *temp = m_head;
while (temp != nullptr){
if (temp -> getNextNode() == nullptr){
cout << temp -> getValue() << endl;
return;
}else{
cout << temp -> getValue() << ",";
temp = temp -> getNextNode();
}
}
return;
}
}
template <class T>
void Vector<T>::Remove(int index){
Node<T> *curr = m_head;
Node<T> *prev = m_head;
int counter = 0;
// travereses linked list until counter is same as the index to remove
while (curr != nullptr){
if (counter == index){
// previous's new node is set to curr's next node ending the link with curr
prev -> setNextNode(curr ->getNextNode);
// deletes curr
delete curr;
return;
}
prev = curr;
curr = curr -> getNextNode();
counter++;
}
}
template<class T>
Vector<char>* Vector<T>::operator<(Vector<T>& other){
Node<T> *temp = m_head;
int counter = 0;
// new dynamically allocated Vector
Vector<char>* new_vec = new Vector<char>();
while (temp != nullptr){
// if this vectors value is less then others at this index new_vec gets a T other false
if (operator[](counter) < other.operator[](counter)){
new_vec -> Insert(TRUE);
}else{
new_vec -> Insert(FALSE);
}
temp = temp ->getNextNode();
counter++;
}
return new_vec;
}
template<class T>
Vector<char>* Vector<T>::operator==(Vector<T>& other){
// runs same as < operator function above just == instead of <
Node<T> *temp = m_head;
int counter = 0;
Vector<char>* new_vec = new Vector<char>();
while (temp != nullptr){
if (operator[](counter) == other.operator[](counter)){
new_vec -> Insert(TRUE);
}else{
new_vec -> Insert(FALSE);
}
temp = temp ->getNextNode();
counter++;
}
return new_vec;
}
template<class T>
Vector<T>* Vector<T>::operator+(Vector<T>& source){
Vector<T>* new_vec = new Vector<T>();
Node<T>* temp = m_head;
int counter = 0;
// if the Size is less or they are equal, adds values together prevents going out of bounds since Size is smallers
if (Size() < source.Size() || Size() == source.Size()){
while (temp != nullptr){
new_vec -> Insert(operator[](counter) + source.operator[](counter));
temp = temp ->getNextNode();
counter++;
}
}else{
// if source's size is less then it will add all of sources index with this vectors to avoid going out of bounds
for (int i = 0; i < source.Size(); i++){
new_vec -> Insert(operator[](i) + source.operator[](i));
}
}
return new_vec;
}
template<class T>
Vector<T>* Vector<T>::operator*(Vector<T>& source){
// runs same as + operator function but is * the values instead
Vector<T>* new_vec = new Vector<T>();
Node<T>* temp = m_head;
int counter = 0;
if (Size() < source.Size() || Size() == source.Size()){
while (temp != nullptr){
new_vec -> Insert(operator[](counter) * source.operator[](counter));
temp = temp ->getNextNode();
counter++;
}
}else{
for (int i = 0; i < source.Size(); i++){
new_vec -> Insert(operator[](i) * source.operator[](i));
}
}
return new_vec;
}
template<class T>
float Vector<T>::Mean(){
Node<T> *temp = m_head;
int counter = 0;
float mean = 0.0;
// traverses Vector and adds all sizes
while (temp != nullptr){
mean += operator[](counter);
temp = temp ->getNextNode();
counter++;
}
// divides mean by size to get actual mean
mean /= Size();
return mean;
}
template<class T>
float Vector<T>::Median(){
// if the size is odd than the meadian can be found by intger dividing the size by 2 and returning that value
if (Size() % TWO != 0){
return operator[](Size() / TWO);
// else take then middle value and one less add them then divide by two to get median
}else{
return float((operator[](Size() / TWO) + operator[]((Size() / TWO) - ONE)) / TWO);
}
}
template<class T>
float Vector<T>::StDev(){
Node<T> *temp = m_head;
float mean = Mean();
float numbers = 0.0;
int counter = 0;
while (temp != nullptr){
// adds the value - mean ^ 2 to numbers
numbers += (operator[](counter) - mean) * (operator[](counter) - mean);
temp = temp ->getNextNode();
counter++;
}
// divides numbers by size then takes square root to get standard deviation
return (sqrt(numbers / Size()));
}
template<class T>
Vector<T>::~Vector(){
Node<T>* current = m_head;
while(current != nullptr){
current = current -> getNextNode();
// deletes the node
delete m_head;
m_head = current;
}
m_head = nullptr;
}
#endif /* VECTOR_CPP */