Skip to content

Commit ae2dd5f

Browse files
committed
implemented remove function
1 parent c066f0a commit ae2dd5f

2 files changed

Lines changed: 71 additions & 33 deletions

File tree

DSDoublyLL/dsdoublyll.h

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -228,24 +228,21 @@ void DSDoublyLL<T>::insert(int index, T data){
228228
return;
229229
}
230230

231-
DSNode<T>* newNode = new DSNode<T>(data);
232-
233-
//ToDo: throw an "index out of bounds" error
234-
DSNode<T>* current = head;
235-
for(int i = 0; i < index - 1; i++){
236-
current = current->next;
231+
if(index == numIndexes){
232+
pushBack(data);
233+
return;
237234
}
238235

239-
newNode->next = current->next;
240-
newNode->prev = current;
236+
DSNode<T>* nodeBefore = getNodeAt(index - 1);
241237

242-
current->next = newNode;
238+
DSNode<T>* newNode = new DSNode<T>(data);
243239

244-
if(newNode->next == nullptr){
245-
tail = newNode;
246-
}else {
247-
newNode->next->prev = newNode;
248-
}
240+
newNode->next = nodeBefore->next;
241+
newNode->prev = nodeBefore;
242+
243+
nodeBefore->next = newNode;
244+
245+
newNode->next->prev = newNode;
249246

250247
numIndexes++;
251248
}
@@ -256,7 +253,41 @@ void DSDoublyLL<T>::insert(int index, T data){
256253
*/
257254
template <class T>
258255
void DSDoublyLL<T>::remove(int index){
259-
//ToDo: remove
256+
if(index == 0){
257+
DSNode<T>* temp = head->next;
258+
delete head;
259+
260+
head = temp;
261+
head->prev = nullptr;
262+
263+
numIndexes--;
264+
return;
265+
}
266+
267+
if(index == numIndexes - 1){
268+
DSNode<T>* temp = tail->prev;
269+
delete tail;
270+
271+
tail = temp;
272+
tail->next = nullptr;
273+
274+
numIndexes--;
275+
return;
276+
}
277+
278+
DSNode<T>* target = getNodeAt(index);
279+
280+
target->prev->next = target->next;
281+
282+
if(target->next != nullptr){
283+
target->next->prev = target->prev;
284+
} else {
285+
tail = target->prev;
286+
}
287+
288+
numIndexes--;
289+
290+
delete target;
260291
}
261292

262293
/**
@@ -326,11 +357,7 @@ void DSDoublyLL<T>::popBack(){
326357
template<class T>
327358
int DSDoublyLL<T>::size()
328359
{
329-
int total = 0;
330-
for(DSNode<T>* current = head; current != nullptr; current = current->next){
331-
total++;
332-
}
333-
return total;
360+
return numIndexes;
334361
}
335362

336363
/**
@@ -457,6 +484,8 @@ template <class T>
457484
DSNode<T>* DSDoublyLL<T>::getNodeAt(int index) const{
458485
DSNode<T>* current;
459486

487+
//ToDo: throw an "index out of bounds" error
488+
460489
// set indexing to start from the closer end node
461490
if(index < 0){
462491
index = numIndexes + index;

test.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,34 +410,43 @@ TEST_CASE("Data_Structures_Doubly_Linked_List", "[Doubly_Linked_List][Data_Struc
410410
REQUIRE(numList1[1] == 1);
411411
}
412412

413+
SECTION("Other Operators"){
414+
// adding
415+
REQUIRE(numList3 == numList1 + numList2);
416+
417+
// adding and setting equal
418+
numList1 += numList2;
419+
REQUIRE(numList3 == numList1);
420+
numList1 = numList1Copy;
421+
422+
// size
423+
REQUIRE(numList1.size() == 3);
424+
}
425+
413426
SECTION("Insertion and Removal"){
427+
// insertion
414428
numList4.insert(0, 0);
415429
numList4.insert(3, 3);
416430
numList4.insert(5, 5);
417431

418432
REQUIRE(numList4 == numList3);
419433

420-
/*numList4.remove(5);
434+
// removal
435+
numList4.remove(5);
421436
numList4.remove(1);
422437
numList4.remove(0);
423438
numList4.remove(0);
424439

425440
numList4.insert(2, 5);
426441

427-
REQUIRE(numList4 == numList2);*/
428-
}
429-
430-
SECTION("Other Operators"){
431-
// adding
432-
REQUIRE(numList3 == numList1 + numList2);
442+
REQUIRE(numList4 == numList2);
433443

434-
// adding and setting equal
435-
numList1 += numList2;
436-
REQUIRE(numList3 == numList1);
437-
numList1 = numList1Copy;
444+
// reset list
445+
numList4.remove(2);
446+
numList4.remove(0);
438447

439-
// size
440-
REQUIRE(numList1.size() == 3);
448+
numList4.pushFront(2);
449+
numList4.pushFront(1);
441450
}
442451

443452
SECTION("popping"){

0 commit comments

Comments
 (0)