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

发布于 2025-01-10 12:58:17 字数 2413 浏览 0 评论 0原文

我应用了合并排序算法来对链表进行排序,但是当我应用它时,它给出了其中存在分段错误(核心转储)。 帮我调试一下。

#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 ,让我们看看它是否有效。

I applied merge sort algorithm to sort a linked list but when I applied it , it's giving there is a segmentation fault(core dumped) in it.
Help me to debug it.

#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;
}

I tried to debug but was not able to find out what's the fault.
I am checking for nullptr,temp as specified in one of the comment , let's see if it work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2025-01-17 12:58:17

最后我调试了一下。
我用 : 替换了 mergeSort 的基本情况

if(temp == NULL || temp->next == NULL){
    return temp;
}

,它起作用了。

Finally, I debugged it.
I replaced base case of mergeSort by :

if(temp == NULL || temp->next == NULL){
    return temp;
}

and it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文