C语言 Merge sort Linked List giving Segmentation fault



我正在尝试使用merge排序对链表进行排序。我创建了两种方法,一种用于分割链表,另一种用于合并分割后的列表,但这会导致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 midn",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进行比较是未定义的行为。

请检查合并的链接列表,其中的关键功能如下所示:

/* 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; 
} 

最新更新