简体   繁体   中英

Why am I getting a segmentation fault when I try to run mergesort on my C++ linked list? (SEGFAULT)

I am implementing a linked list with a merge sort function for a class project. My program compiles, but when I try to run it I get segmentation fault(core dumped). I debugged my program using GDB, and found that the segfault happens with the pointer frontRef and backRef in my listSplit() function (line 98 in the code below).

Can someone please help me? For the life of me I can't figure out why I am getting a segfault. I would greatly appreciate help with this.

#include "orderedList.h"

orderedList::orderedList() {
  listLength = 0;
  traversalCount = 0;
  head = nullptr;
  tail = nullptr;
}
    
void orderedList::add(int n) {
  listLength++;
  struct node* point = new node;
  point->value = n;
  point->next = nullptr;

 
  if (head == nullptr) {
    head = point;
    tail = point;
  }
  else {
    point->next = head;
    head = point;
  }
}

void orderedList::merge(struct node** headRef) {
  struct node *listHead = *headRef;
  struct node *a;
  struct node *b;

  if ((listHead == nullptr) || (listHead->next == nullptr)) {
    return;
  }

  listSplit(listHead, &a, &b);
  merge(&a);
  merge(&b);

  *headRef = sortedMerge(a, b);
}
 
orderedList::node* orderedList::sortedMerge(struct node* a, struct node *b)
{
  struct node* result = nullptr;


  if (a == nullptr) {
    return (b);
  }
  if (b == nullptr) {
    return (a);
  }

  if (a->value <= b->value) {
    result = a;
    result->next = sortedMerge(a->next, b);
  }
  else {
    result = b;
    result->next = sortedMerge(a, b->next);
  }
  return (result);
}

void orderedList::print() {
  struct node* temp = head;
  while (temp != nullptr) {
    std::cout << temp->value << " ";
    temp = temp->next;
  }
  delete(temp);
}

int orderedList::search(int key) {
 
  int traversals = 1; 
 
  struct node* current = head;
  struct node* previous = nullptr;

  while (current != nullptr) {
    if (current->value == key) {
      if (previous != nullptr) {
        previous->next = current->next;
        current->next = head;
        head = current;
        return traversals;
      }
    }
    previous = current;
    current = current->next;
    traversals ++;
  }
  return 1;
}

void orderedList::listSplit(struct node* source, struct node** frontRef, struct node** backRef) {    // <--- Line 98
  struct node* current = source;
  int hopCount = ((listLength - 1) / 2);
  for (int i = 0; i < hopCount; i++) {
    current = current->next;
  }

  *frontRef = source;
  *backRef = current->next;
  current->next = nullptr;
}

You made *backRef point to current->next and then you let current->next = nullptr. This makes *backRef pointing to a nullptr. Did you later try to do something with the returned backRef, aka a node variable in your caller code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM