Skip to content

Commit d0c6f5c

Browse files
committed
Added size function
1 parent 7409d2b commit d0c6f5c

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

DSDoublyLL/dsdoublyll.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class DSDoublyLL{
5353
*/
5454
void popBack();
5555

56+
/**
57+
* @brief size - returns the size of the list
58+
* @return size of the list
59+
*/
60+
int size();
61+
5662
/**
5763
* @brief DSDoublyLL::operator [] - returns data at passed index
5864
* @param index - index to retrieve data
@@ -246,6 +252,16 @@ void DSDoublyLL<T>::popBack(){
246252
tail->next = nullptr;
247253
}
248254

255+
template<class T>
256+
int DSDoublyLL<T>::size()
257+
{
258+
int total = 0;
259+
for(DSNode<T>* current = head; current != nullptr; current = current->next){
260+
total++;
261+
}
262+
return total;
263+
}
264+
249265
template <class T>
250266
void DSDoublyLL<T>::insert(T data, int index){
251267
//ToDo: insert

test.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,16 @@ TEST_CASE("Data_Structures_Test", "[Data_Structures_Test]"){
386386
}
387387

388388
SECTION("Other Operators"){
389+
// adding
389390
REQUIRE(numList3 == numList1 + numList2);
390391

392+
// adding and setting equal
391393
numList1 += numList2;
392394
REQUIRE(numList3 == numList1);
393-
394395
numList1 = numList1Copy;
396+
397+
// size
398+
REQUIRE(numList1.size() == 3);
395399
}
396400

397401
SECTION("popping"){

0 commit comments

Comments
 (0)