我应用了合并排序算法对链表进行排序,但当我应用它时,它给出了一个分段错误(核心转储(。帮我调试一下。
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int data){
this->data = data;
next = NULL;
}
};
Node *takeInput(int n){
int data;
Node *head = NULL;
Node *tail = NULL;
while(n>0){
cin >> data;
Node *newNode = new Node(data);
if(head == NULL){
head = newNode;
tail = newNode;
}else{
tail->next = newNode;
tail = newNode;
}
n--;
}
return head;
}
Node *merge(Node *head , Node *head2){
Node *newhead = NULL;
Node *newTail = NULL;
while(head != NULL && head2 != NULL){
int x = head->data;
int y = head2->data;
if(x < y){
if(newhead == NULL){
newhead = head ;
newTail = head ;
}else{
newTail->next = head;
newTail = head;
}
head = head->next;
}else{
if(newhead == NULL){
newhead = head2 ;
newTail = head2 ;
}else{
newTail->next = head2;
newTail = head2;
}
head2 = head2->next;
}
}
if(head == NULL){
newTail->next = head2;
}else{
newTail->next = head;
}
return newhead;
}
Node *mergeSort(Node *head, int n){
if(n == 0){
head->next =NULL;
return head;
}else{
int t = (n-1)/2;
Node *temp = head;
while(t>0){
temp = temp->next;
t--;
}
Node *head2 = temp->next;
temp->next = NULL;
head = mergeSort(head,n/2);
int n2 = n - n/2 ;
head2 = mergeSort(head2,n2);
Node *newHead = merge(head,head2);
return newHead;
}
}
void print(Node *head){
while(head != NULL){
cout << head->data << " ";
head = head->next;
}
}
int main()
{
int n;
cin >> n ;
Node *head = takeInput(n);
Node *newHead = mergeSort(head,n);
print(newHead);
return 0;
}
我试着调试,但没能找出故障所在。我正在检查其中一条注释中指定的nullptr
、temp
,让我们看看它是否有效。
最后,我对它进行了调试。我将mergeSort的基本情况替换为:
if(temp == NULL || temp->next == NULL){
return temp;
}
它奏效了。