首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并排序链表出现分割错误

合并排序链表出现分割错误
EN

Stack Overflow用户
提问于 2020-09-16 03:37:15
回答 1查看 137关注 0票数 1

我正在尝试使用合并排序对链表进行排序。我已经创建了两个方法,一个用于划分链表,另一个用于合并划分的列表,但它给出了分割错误。下面是排序和合并的代码。我找不到我的代码在哪里出现了分段错误。请帮帮忙。谢谢。

代码语言:javascript
复制
       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;
    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-17 02:45:26

关于:

代码语言:javascript
复制
struct Node *head;     
if(head==NULL) 

指针头未设置为任何已知值。因此,将其与NULL进行比较是未定义的行为。

请检查mergesort for linked list,其中的关键函数如下所示:

代码语言:javascript
复制
/* 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; 
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63908763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档