Skip to content

Commit 37adc55

Browse files
authored
Merge pull request #9 from Levi-B4/DSDoublyLL_+operation
implemented operator+(T) and operator+=(T) for DSDoublyLL
2 parents 9a3602e + 6311f53 commit 37adc55

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

DSDoublyLL/dsdoublyll.h

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,26 @@ class DSDoublyLL{
215215
DSDoublyLL<T> operator+(const DSDoublyLL<T>& other) const;
216216

217217
/**
218-
* @brief operator += : combines this
219-
* @param other
220-
* @return
218+
* @brief operator + : returns a linked list combining this list with the given element
219+
* @param other - reference to given the element
220+
* @return combined linked list of this and given list
221+
*/
222+
DSDoublyLL<T> operator+(const T& other) const;
223+
224+
/**
225+
* @brief operator += : combines this with the given linked list
226+
* @param other: referrence to the linked list to combine with this
227+
* @return this
221228
*/
222229
DSDoublyLL<T>& operator+=(const DSDoublyLL<T>& other);
223230

231+
/**
232+
* @brief operator += : returns a linked list combining this list with the given element
233+
* @param other - reference to the element to append to this
234+
* @return this
235+
*/
236+
DSDoublyLL<T>& operator+=(const T& other);
237+
224238
/**
225239
* @brief operator == : compares this list against another list
226240
* @param other - reference to target list
@@ -776,6 +790,24 @@ DSDoublyLL<T> DSDoublyLL<T>::operator+(const DSDoublyLL<T>& other) const{
776790
return result;
777791
}
778792

793+
/**
794+
* @brief operator + : returns a linked list combining this list with the given element
795+
* @param data - reference to given the element
796+
* @return combined linked list of this and given list
797+
*/
798+
template <class T>
799+
DSDoublyLL<T> DSDoublyLL<T>::operator+(const T& data) const{
800+
DSDoublyLL<T> result;
801+
802+
for(DSNode<T>* current = this->head; current != nullptr; current = current->next){
803+
result.pushBack(current->data);
804+
}
805+
806+
result.pushBack(data);
807+
808+
return result;
809+
}
810+
779811
/**
780812
* @brief operator += : sets data equal to this and the given doubly linked list
781813
* @param other - the list to be added to this
@@ -790,6 +822,19 @@ DSDoublyLL<T>& DSDoublyLL<T>::operator+=(const DSDoublyLL<T>& other){
790822
return *this;
791823
}
792824

825+
/**
826+
* @brief operator += : returns a linked list combining this list with the given element
827+
* @param other - reference to the element to append to this
828+
* @return data
829+
*/
830+
template <class T>
831+
DSDoublyLL<T>& DSDoublyLL<T>::operator+=(const T& data){
832+
pushBack(data);
833+
834+
return *this;
835+
}
836+
837+
793838
/**
794839
* @brief operator == : compares this list against another list
795840
* @param other - reference to target list

test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,20 @@ TEST_CASE("Data_Structures_Doubly_Linked_List", "[Doubly_Linked_List][Data_Struc
339339
SECTION("Other Operators"){
340340
// adding
341341
REQUIRE(numList3 == numList1 + numList2);
342+
REQUIRE(numList3 == numList1 + 3 + 4 + 5);
342343

343344
// adding and setting equal
344345
numList1 += numList2;
345346
REQUIRE(numList3 == numList1);
346347
numList1 = numList1Copy;
347348

349+
numList1 += 3;
350+
numList1 += 4;
351+
numList1 += 5;
352+
REQUIRE(numList3 == numList1);
353+
numList1 = numList1Copy;
354+
355+
348356
// size
349357
REQUIRE(numList1.size() == 3);
350358
}

0 commit comments

Comments
 (0)