0

I am sorry to bother, but i really need some help.Having been struggling for hours. Here is some code i wrote for my assignment. My assignment is to implement a template function called merge which used to merged two double linkedlist into a new list.

But it turned to "SIGSEGV - Segmentation violation signal" when testing my code in a case below:

TEST_CASE("Testing merge: Left and right lists non-empty; same size", "[weight=1]") {

  LinkedList<int> left;
  left.pushBack(1);
  left.pushBack(5);
  left.pushBack(10);
  left.pushBack(20);
  LinkedList<int> right;
  right.pushBack(2);
  right.pushBack(4);
  right.pushBack(11);
  right.pushBack(19);
  LinkedList<int> expectedList;
  expectedList.pushBack(1);
  expectedList.pushBack(2);
  expectedList.pushBack(4);
  expectedList.pushBack(5);
  expectedList.pushBack(10);
  expectedList.pushBack(11);
  expectedList.pushBack(19);
  expectedList.pushBack(20);
  auto studentResultList = left.merge(right);

  SECTION("Checking that values are correct") {
    REQUIRE(studentResultList == expectedList);
  }

  SECTION("Checking that the list prev links and tail pointer are being set correctly") {
    REQUIRE(studentResultList.assertPrevLinks());
  }

  SECTION("Checking that the list size is being tracked correctly") {
    REQUIRE(studentResultList.assertCorrectSize());
  }
}

//I can't figure out what is wrong.Below is my code:

   //this is a member function
   template <typename T>
   LinkedList<T> LinkedList<T>::merge(const LinkedList<T>& other) const {
   
     LinkedList<T> left = *this;
     LinkedList<T> right = other;
   
     LinkedList<T> merged;
   
     Node* temp = merged.head_;
     Node* temp1 = left.head_;
     Node* temp2 = right.head_;
     
     //check if one of the list is empty.
     if(!left.head_){
       merged = right;
       return merged;
     }
     if(!right.head_){
       merged = left;
       return merged;
     }
     
     //determine the first node for the new merged list
     if(temp1->data >= temp2->data){
       temp->data = temp2->data;
       temp2 = temp2->next;
       
     }
     else{
       temp->data = temp1->data;
       temp1 = temp1->next;
     }
     
     //loop until  the end of one list
     while(temp1 && temp2){
       if(temp1->data >= temp2->data){
         temp->next->data = temp2->data;
         temp2 = temp2->next;
       }
       else{
         temp->next->data = temp1->data;
         temp1 = temp1->next;
       }
       temp = temp->next;
     }
   
     //loop over the not empty list 
     while(!temp1 && temp2){
        temp->next->data = temp2->data;
        temp2 = temp2->next;
        temp = temp->next;
      }
     
     while(!temp2 && temp1){
        temp->next->data = temp1->data;
        temp1 = temp1->next;
        temp = temp->next;
     }
     
     return merged;
   }

phoebe lim
  • 11
  • 1
  • 1
    My guess would be https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three please show a [mre]. Also i assume in `temp->next->data` `temp->next` is null – Alan Birtles Mar 24 '22 at 07:09
  • Based solely on the number of copies of LinkedList objects being made in this code, my money is on a [Rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three) violation. Without sufficient code it is impossible to say otherwise. – WhozCraig Mar 24 '22 at 07:12
  • Better, but still incomplete, so we can't even compile it and reproduce it: https://godbolt.org/z/EPMWK4fzn – Marek R Mar 24 '22 at 07:46
  • My recommendation is: when crash happens in debug mode, find "call stack" window, click through each entry and try analyze code pointed by each entry. – Marek R Mar 24 '22 at 07:52
  • I see problem: you are creating empty list `merged` and assuming that head is not null (and it should be null). Your do nod add new items to `merged` in proper way. – Marek R Mar 24 '22 at 08:00
  • test cases: 8 | 7 passed | 1 failed; assertions: 25 | 24 passed | 1 failed ; There is 8 test cases.and just one test case(test code is above in my post) failed.Maybe adding new items is ok in my code?@MarekR. I wish I could post the whole definition file of the LinkedList.but the file is over the limit and i am afraid that putting the whole assignment code here may not be allowed. Sorry that i am really new in c++ and i am not that good at English. – phoebe lim Mar 24 '22 at 08:15
  • thank you guys. it seems it's the null problem.i new a node then it's solved! thanks a lot – phoebe lim Mar 24 '22 at 12:26

0 Answers0