我正在尝试使用合并排序对链表进行排序。我已经创建了两个方法,一个用于划分链表,另一个用于合并划分的列表,但它给出了分割错误。下面是排序和合并的代码。我找不到我的代码在哪里出现了分段错误。请帮帮忙。谢谢。
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;
}发布于 2020-09-17 02:45:26
关于:
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;
} https://stackoverflow.com/questions/63908763
复制相似问题