Skip to content

Commit 137e4b3

Browse files
committed
implemented adding and removing nodes
1 parent 4856477 commit 137e4b3

3 files changed

Lines changed: 288 additions & 244 deletions

File tree

DSAdjList/dsadjlist.h

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,34 @@ void DSAdjList<T>::addNode(T nodeData){
143143
*/
144144
template <class T>
145145
void DSAdjList<T>::addEdge(T node1, T node2){
146-
int nodesFound = 0;
147-
for(auto list = data.begin(); nodesFound != 2; list++){
148-
if((*list)[0] == node1){
149-
if(list->contains(node2)){
146+
bool node1Found = false;
147+
bool node2Found = false;
148+
149+
for(auto i = data.begin(); i != data.end() && (!node2Found || !node1Found); i++){
150+
if((*i)[0] == node1){
151+
if(i->contains(node2)){
150152
return;
151153
}
152-
list->pushBack(node2);
153-
nodesFound++;
154-
} else if((*list)[0] == node2){
155-
if(list->contains(node1)){
154+
i->pushBack(node2);
155+
node1Found = true;
156+
} else if((*i)[0] == node2){
157+
if(i->contains(node1)){
156158
return;
157159
}
158-
list->pushBack(node1);
159-
nodesFound++;
160+
i->pushBack(node1);
161+
node2Found = true;
160162
}
161163
}
164+
165+
if(!node1Found){
166+
addNode(node1);
167+
data[data.size() - 1].pushBack(node2);
168+
}
169+
170+
if(!node2Found){
171+
addNode(node2);
172+
data[data.size() - 1].pushBack(node1);
173+
}
162174
}
163175

164176
/**
@@ -167,6 +179,14 @@ void DSAdjList<T>::addEdge(T node1, T node2){
167179
*/
168180
template <class T>
169181
void DSAdjList<T>::removeNode(const T nodeData){
182+
for(auto i = data.begin(); i != data.end();){
183+
if((*i)[0] == nodeData){
184+
i = data.removeAt(i);
185+
} else {
186+
i->remove(nodeData, true);
187+
i++;
188+
}
189+
}
170190
}
171191

172192
/**
@@ -176,6 +196,13 @@ void DSAdjList<T>::removeNode(const T nodeData){
176196
*/
177197
template <class T>
178198
void DSAdjList<T>::removeEdge(const T node1, const T node2){
199+
for(auto& list : data){
200+
if(list[0] == node1){
201+
list.remove(node2, true);
202+
} else if(list[0] == node2){
203+
list.remove(node1, true);
204+
}
205+
}
179206
}
180207

181208
/**
@@ -195,7 +222,6 @@ bool DSAdjList<T>::contains(T query)
195222
return false;
196223
}
197224

198-
//TODO: may make this faster to replace the data as you go through rather than clear the whole list
199225
/**
200226
* @brief operator = : sets this list equal to the list passed in
201227
* @param other - reference to target list
@@ -251,7 +277,7 @@ bool DSAdjList<T>::operator==(const DSAdjList<T>& other) const{
251277
*/
252278
template <class T>
253279
bool DSAdjList<T>::operator!=(const DSAdjList<T>& other) const{
254-
return !*this==other;
280+
return !(*this == other);
255281
}
256282

257283
/**

0 commit comments

Comments
 (0)