Skip to content

Commit c066f0a

Browse files
committed
implemented insert function
1 parent 5e5e9cd commit c066f0a

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

DSDoublyLL/dsdoublyll.h

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class DSDoublyLL{
1818
*/
1919
DSDoublyLL(const T* data, const int size);
2020

21+
/**
22+
* @brief DSDoublyLL - copy constructor
23+
* @param other - reference of list to copy
24+
*/
25+
DSDoublyLL(const DSDoublyLL<T>& other);
26+
27+
2128
/**
2229
* @brief getNumIndexes - Getter: numIndexes
2330
* @return numIndexes
@@ -146,6 +153,17 @@ DSDoublyLL<T>::DSDoublyLL(const T* data, const int size){
146153
}
147154
}
148155

156+
/**
157+
* @brief DSDoublyLL - copy constructor
158+
* @param other - reference to the list to copy
159+
*/
160+
template <class T>
161+
DSDoublyLL<T>::DSDoublyLL(const DSDoublyLL& other){
162+
for(DSNode<T>* n = other.head; n != nullptr; n = n->next){
163+
pushBack(n->data);
164+
}
165+
}
166+
149167

150168
/**
151169
* @brief getNumIndexes - Getter: numIndexes
@@ -214,21 +232,22 @@ void DSDoublyLL<T>::insert(int index, T data){
214232

215233
//ToDo: throw an "index out of bounds" error
216234
DSNode<T>* current = head;
217-
for(int count = 0; count < index - 1; current = current->next){
218-
count++;
235+
for(int i = 0; i < index - 1; i++){
236+
current = current->next;
219237
}
220238

221-
if(current->next == nullptr){
222-
tail = newNode;
223-
current->next = newNode;
224-
newNode->prev = current;
225-
} else {
226-
current->next->prev = newNode;
227-
newNode->next = current->next;
239+
newNode->next = current->next;
240+
newNode->prev = current;
228241

229-
current->next = newNode;
230-
newNode->prev = current;
242+
current->next = newNode;
243+
244+
if(newNode->next == nullptr){
245+
tail = newNode;
246+
}else {
247+
newNode->next->prev = newNode;
231248
}
249+
250+
numIndexes++;
232251
}
233252

234253
/**

test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,18 +415,16 @@ TEST_CASE("Data_Structures_Doubly_Linked_List", "[Doubly_Linked_List][Data_Struc
415415
numList4.insert(3, 3);
416416
numList4.insert(5, 5);
417417

418-
for (int i = 0; i < numList4.size(); i++) {
419-
std::cout << "Element " << i << ": " << numList4[i] << std::endl;
420-
}
418+
REQUIRE(numList4 == numList3);
421419

422-
numList4.remove(5);
420+
/*numList4.remove(5);
423421
numList4.remove(1);
424422
numList4.remove(0);
425423
numList4.remove(0);
426424
427425
numList4.insert(2, 5);
428426
429-
REQUIRE(numList4 == numList2);
427+
REQUIRE(numList4 == numList2);*/
430428
}
431429

432430
SECTION("Other Operators"){

0 commit comments

Comments
 (0)