合并排序链表给出分段错误

Merge sort Linked List giving Segmentation fault

我正在尝试使用合并排序对链表进行排序。我创建了 2 种方法,一种用于划分链表,一种用于合并划分后的列表,但它给出了 Segmentation Fault。下面是排序和合并的代码。我无法找到我的代码出现分段错误的位置。请帮忙。谢谢

       struct Node *merge(struct Node *first,struct Node *second)
{
    struct Node *temp;
    if(first==NULL)
    return second;
    if(second==NULL)
    return first;
    struct Node *head;
    if(head==NULL)
    {
        if(first->data<=second->data)
        {
            temp=first;
            first=first->next;
        }
        else
        {
            temp=second;
            second=second->next;
        }
        head=temp;
    }
    
    while(first!=NULL && second!=NULL)
    {
        if(first->data<=second->data)
        {
            temp->next=first;
            temp=first;
            first=first->next;
        }
        else
        {
            temp->next=second;
            temp=second;
            second=second->next;
        }
    }
    
   if(first!=NULL)
    {
        temp->next=first;
    }
    
    if(second!=NULL)
    {
        temp->next=second;
    }
    
    return head;
} 

Node* mergeSort(Node* head) {
    // your code here
   if(head==NULL || head->next==NULL)
   return head;
   struct Node *fptr, *sptr;
   sptr=head;
   fptr=head->next;
   while(fptr && fptr->next!=NULL)
   {
           sptr=sptr->next;
           fptr = fptr->next->next;
   }
   
   struct Node *mid = sptr;
   printf("%d is mid\n",mid->data);
   struct Node* mid_next = sptr->next;
   mid->next=NULL;
   
   struct Node *first = mergeSort(head);
   struct Node *second = mergeSort(mid_next);
   
    struct Node *list= merge(first,second);
    return list;
    
}

关于:

struct Node *head;     
if(head==NULL) 

指针头尚未设置为任何已知值。所以将它与 NULL 进行比较是未定义的行为。

请检查 mergesort for linked list 下面列出的主要功能:

/* Link list node */
struct Node { 
    int data; 
    struct Node* next; 
}; 
  
/* function prototypes */
struct Node* SortedMerge(struct Node* a, struct Node* b); 
void FrontBackSplit(struct Node* source, 
                    struct Node** frontRef, struct Node** backRef); 
  
/* sorts the linked list by changing next pointers (not data) */
void MergeSort(struct Node** headRef) 
{ 
    struct Node* head = *headRef; 
    struct Node* a; 
    struct Node* b; 
  
    /* Base case -- length 0 or 1 */
    if ((head == NULL) || (head->next == NULL)) { 
        return; 
    } 
  
    /* Split head into 'a' and 'b' sublists */
    FrontBackSplit(head, &a, &b); 
  
    /* Recursively sort the sublists */
    MergeSort(&a); 
    MergeSort(&b); 
  
    /* answer = merge the two sorted lists together */
    *headRef = SortedMerge(a, b); 
} 
  
/* See https:// www.geeksforgeeks.org/?p=3622 for details of this  
function */
struct Node* SortedMerge(struct Node* a, struct Node* b) 
{ 
    struct Node* result = NULL; 
  
    /* Base cases */
    if (a == NULL) 
        return (b); 
    else if (b == NULL) 
        return (a); 
  
    /* Pick either a or b, and recur */
    if (a->data <= b->data) { 
        result = a; 
        result->next = SortedMerge(a->next, b); 
    } 
    else { 
        result = b; 
        result->next = SortedMerge(a, b->next); 
    } 
    return (result); 
} 
  
/* UTILITY FUNCTIONS */
/* Split the nodes of the given list into front and back halves, 
    and return the two lists using the reference parameters. 
    If the length is odd, the extra node should go in the front list. 
    Uses the fast/slow pointer strategy. */
void FrontBackSplit(struct Node* source, 
                    struct Node** frontRef, struct Node** backRef) 
{ 
    struct Node* fast; 
    struct Node* slow; 
    slow = source; 
    fast = source->next; 
  
    /* Advance 'fast' two nodes, and advance 'slow' one node */
    while (fast != NULL) { 
        fast = fast->next; 
        if (fast != NULL) { 
            slow = slow->next; 
            fast = fast->next; 
        } 
    } 
  
    /* 'slow' is before the midpoint in the list, so split it in two 
    at that point. */
    *frontRef = source; 
    *backRef = slow->next; 
    slow->next = NULL; 
}