【问题标题】:MergeSort with Linked List - Split function C++ [closed]MergeSort 与链表 - 拆分函数 C++ [关闭]
【发布时间】:2012-04-17 23:34:53
【问题描述】:

我的 MergeSort 类中的拆分函数有问题。它适用于前几个,但后来出现分段错误。我认为这是我的 for 循环并选择了正确的中间节点,但我无法弄清楚。

感谢任何帮助,这是我的代码:


标题:

#ifndef MERGE_LIST
#define MERGE_LIST

#include <iostream>
using namespace std;

class mergeList {
    public:
        struct node {
            int data;
            struct node* next;
        };

        mergeList();
        void push( struct node** head_ref, int new_data );
        void printList( struct node * nptr );
        //void merge( struct node** headRef );
        struct node* SortedMerge( struct node* a, struct node* b );
        void merge(struct node** headRef);

    private:
        int size;
        //void merge( struct node** headRef, int s );
        //void split( struct node* headRef, struct node** a, struct node** b, int mid );
        void split(struct node* source, struct node** frontRef, struct node** backRef, int s);
        void merge(struct node** headRef, int s);
};

#endif

来源:

#include "mergeList.h"
#include "stdlib.h"

mergeList::mergeList( ) {
    size = 0;
}

void mergeList::push( struct node** head_ref, int new_data ) {

    struct node* new_node = ( struct node* ) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = (*head_ref);    
    (*head_ref)    = new_node;
    size++;
}

void mergeList::printList( struct node * nptr ) {

    while( nptr ) {
        cout << nptr->data << " ";
        nptr=nptr->next;
    }
    cout << endl;
}

void mergeList::merge(struct node** headRef) {
    merge( headRef, size);
}

void mergeList::merge(struct node** headRef, int s)
{

    if( s < 2 )
        return;

    struct node* a;
    struct node* b;
    struct node* head = *headRef;

    bool addOne = false;
    int mid = s/2;

    if( s % 2 != 0 )
        addOne = true;  

    cout << "B4 SPLIT!" << endl;
    cout << "AddOne: "  << addOne << endl;
    cout << "s: " << s << endl;

    split(head, &a, &b, s);

    merge(&a, mid);

    //if( addOne )
    //  mid++;

    merge(&b, mid);

    *headRef = SortedMerge(a, b);
}

//Use a pointer to split the list
void mergeList::split(struct node* headRef, struct node** _a, struct node** _b, int s) {

    struct node* a;
    //struct node* b;
    if ( s < 2) {
        *_a = headRef;
        *_b = NULL;
    }
    else {
        a = headRef;

    if( s != 2 ) {
        for( int i = 0; i < s/2; i++ )
            a = a->next;
    }
            *_a = headRef;
            *_b = a->next;
        a->next = NULL;
    }
}

struct mergeList::node* mergeList::SortedMerge( struct node* a, struct node* b ) {

    struct node* result = NULL; 

    if ( a == NULL )
        return b;
    else if ( b == NULL )
        return a;
    if( a->data <= b->data ) {
        result = a;
        result->next = SortedMerge( a->next, b );
    }
    else {
        result = b;
        result->next = SortedMerge( a, b->next );
    }

    return( result );
}

【问题讨论】:

    标签: c++ linked-list mergesort


    【解决方案1】:

    您是否在调试器(例如 gdb 或 dbx)中运行以查看它在哪里出现了段错误?

    【讨论】:

    • 我真的不知道如何使用它,但我知道它在 for 循环中或之后的某个位置
    • @David 那么你应该认为这是一个学习使用调试工具的好机会。如果你继续编程,你将不得不在某个时候学习。
    • 和kappamaki一样的感觉。它们很容易用来做一些简单的事情,比如捕获段错误或找出无限循环的位置。谷歌“gdb 备忘单”开始。您需要使用 -g 标志进行编译,以告诉编译器保留调试信息。这将准确地告诉您问题所在的行,并为您节省数小时的时间,而不是倾注源代码甚至添加调试语句。使用调试器 = 好懒惰,推迟学习 = 坏懒惰。
    • 我还不是很熟悉,但我拿到了备忘单并通过并找到了错误。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    相关资源
    最近更新 更多