-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyList.cpp
More file actions
251 lines (234 loc) · 5.11 KB
/
MyList.cpp
File metadata and controls
251 lines (234 loc) · 5.11 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
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "proj6.h"
// personal linked list
// default constructor for MyList
MyList::MyList() {
head = nullptr;
tail = nullptr;
size = 0;
}
// MyStack destructor to have clean valgrind report (hopefully)
MyList::~MyList() {
clear();
}
// copy constructor for functions because i get garbage values ;(
MyList::MyList(const MyList& other) {
// I HAD TO SET THIS TO AVOID GARBAGE VAL?????
head = nullptr;
tail = nullptr;
size = 0;
if (other.head == nullptr) {
head = nullptr;
tail = nullptr;
size = 0;
}
else {
// deep copy
myNode* cur = other.head;
while (cur != nullptr) {
this->push_back(cur->getValue());
cur = cur->getNext();
}
this->size = other.size;
}
}
// a copy constructor (overloaded =) to perform deep copy of list
MyList& MyList::operator=(const MyList& other) {
// check if same list return current list
if (this == &other) {
return *this;
}
// delete current contents if not empty
if (!this->isEmpty()) {
this->clear();
}
// perform deep copy
myNode* cur = other.head;
while (cur != nullptr) {
this->push_back(cur->getValue());
cur = cur->getNext();
}
// copy size
this->size = other.size;
return *this;
}
// to alter the head pointer for MyList
void MyList::setHead(myNode* nNode) {
head = nNode;
}
// to alter the tail pointer for MyList
void MyList::setTail(myNode* nNode) {
tail = nNode;
}
// returns current head pointer of MyList
myNode* MyList::getHead() {
return head;
}
// returns current tail pointer of MyList
myNode* MyList::getTail() {
return tail;
}
// returns current size
int MyList::getSize() {
return size;
}
// returns T/F depending if MyList is currently empty
bool MyList::isEmpty() {
if (head == nullptr) {
return true;
}
return false;
}
// adds a node of given value to front of MyList
void MyList::push_front(int value) {
// creating a new node
myNode* nNode = new myNode();
nNode->setValue(value);
nNode->setNext(nullptr);
// check if empty node
if (isEmpty()) {
// single node is now both head and tail
head = nNode;
tail = nNode;
size++;
return;
}
nNode->setNext(head);
head = nNode;
size++;
}
// adds a node of given value to the back of MyList
void MyList::push_back(int value) {
// creating a new node
myNode* nNode = new myNode();
nNode->setValue(value);
nNode->setNext(nullptr);
// check if empty node
if (isEmpty()) {
// printf("I HATE YOUR FRIKIN GUTS\n");
// single node is now both head and tail
head = nNode;
tail = nNode;
size++;
return;
}
// For some reason my tail is NULL...?
// if (tail == nullptr) {
// printf("this is the head while tail = NULL: %d\n", head->getValue());
// printf("TAIL = NULL\n");
// }
// printf("this is nNode: %d\nThis is prevHead: %d\n", nNode->getValue(), head->getValue());
tail->setNext(nNode); // WHY U SEG FAULTING (b/c its null for some reason)
tail = nNode;
size++;
}
// removes the first instance of the node of given value
void MyList::remove(int value) {
// check if there's nothing to remove
if (isEmpty()) {
return;
}
// check if it is the only node
if (head == tail) {
// remove the only node
if (head->getValue() == value) {
delete head;
head = nullptr;
tail = nullptr;
size--;
}
// otherwise, the node to remove DNE
return;
}
myNode* cur = head;
myNode* prev = nullptr;
while (cur != nullptr) {
if (cur->getValue() == value) {
// if node to delete is first node & cur == head
if (prev == nullptr) {
head = head->getNext();
delete cur;
size--;
return;
}
// if node to delete is the last node
else if (cur == tail) {
tail = prev;
prev->setNext(nullptr);
delete cur;
size--;
return;
}
// node to delete is in middle of list
else {
prev->setNext(cur->getNext());
delete cur;
size--;
return;
}
}
prev = cur;
cur = cur->getNext();
}
}
// T/F based on whether the value already exists in list
bool MyList::exist(int val) {
myNode* cur = head;
while (cur != nullptr) {
if (cur->getValue() == val) {
return true;
}
cur = cur->getNext();
}
return false;
}
// returns the value of the given ith node
int MyList::getNthVal(int pos) {
if (pos < 0 || pos >= size) {
try {
throw pos;
}
catch (int e) {
printf("position is out of bounds\n");
}
}
if (isEmpty()) {
try {
throw pos;
}
catch (int e) {
printf("MyList is empty\n");
}
}
// if only one node list
if (head == tail) {
return head->getValue();
}
if (pos == 0) {
return head->getValue();
}
if (pos == (size - 1)) {
return tail->getValue();
}
myNode* cur = head->getNext();
int counter = 1;
while (counter != pos) {
cur = cur->getNext();
counter++;
}
return cur->getValue();
}
void MyList::clear() {
myNode* cur = head;
myNode* temp = nullptr;
while (cur != nullptr) {
temp = cur;
cur = cur->getNext();
delete temp;
}
head = nullptr;
tail = nullptr;
size = 0;
}