【问题标题】:Standard MergeSort Algorithm标准合并排序算法
【发布时间】:2017-03-16 21:53:58
【问题描述】:

我目前正在处理一个非常标准的合并排序文件,但遇到了一些问题。我对数据结构相当陌生,所以并不完全了解发生了什么。任何提示也会很好。谢谢。

这里是代码

#include <stdio.h>
#include <stdlib.h>

typedef struct CELL *LIST;
struct CELL {
    int element;
    LIST next;
}

LIST merge(LIST list1, LIST list2);
LIST split(LIST list);
LIST MergeSort(LIST list);
LIST MakeList();
void PrintList(LIST list);

int main()
{
    LIST list;

    list = MakeList();
    PrintList(MergeSort(list));
}

LIST MakeList()
{
    int x;
    LIST pNewCell;
    if(scanf("%d", &x) == EOF) return NULL;
    else {
        pNewCell = (LIST) malloc(sizeof(struct CELL));
        pNewCell->next = MakeList();
        pNewCell->element = x;
        return pNewCell;
    }
}

void PrintList(LIST list)
{
    while(list != NULL) {
        printf("%d\n", list->element);
        list = list->next;
    }
}

LIST MergeSort(LIST list)
{
    LIST SecondList;

    if (list == NULL) return NULL;
    else if (list->next == NULL) return list;
    else {
        SecondList = split(list);
        return merge(MergeSort(list), MergeSort(SecondList));
    }
}

LIST merge(LIST list1, LIST list2)
{
    if (list1 == NULL) return list2;
    else if(list2 == NULL) return list1;
    else if(list1->element <= list2->element){
        list1->next = merge(list1->next, list2);
        return list1;
    }
    else {
        list2->next = merge(list1, list2->next);
        return list2;
    }
}

LIST split(LIST list)
{
    LIST pSecondCell;

    if (list == NULL) return NULL;
    else if (list->next == NULL) return NULL;
    else {
        pSecondCell = list->next;
        list->next = pSecondCell->next;
        pSecondCell->next = split(pSecondCell->next);
        return pSecondCell;
    }
}

我得到的错误(在多个平台上)是

prog.c:10:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘merge’
 LIST merge(LIST list1, LIST list2);
      ^~~~~
prog.c: In function ‘MergeSort’:
prog.c:53:16: warning: implicit declaration of function ‘merge’ [-Wimplicit-function-declaration]
         return merge(MergeSort(list), MergeSort(SecondList));
                ^~~~~
prog.c:53:16: warning: return makes pointer from integer without a cast [-Wint-conversion]
         return merge(MergeSort(list), MergeSort(SecondList));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c: At top level:
prog.c:57:6: error: conflicting types for ‘merge’
 LIST merge(LIST list1, LIST list2)
      ^~~~~
prog.c:53:16: note: previous implicit declaration of ‘merge’ was here
         return merge(MergeSort(list), MergeSort(SecondList));
                ^~~~~

【问题讨论】:

  • 递归合并会占用 O(n) 的堆栈空间,这对学习来说不是问题,但对于大列表来说会是个问题。如果有兴趣,您可以考虑实现bottom up merge sort for lists - wiki example,它使用相同的merge() 函数,但使用指向节点的指针数组来存储临时列表而不是拆分它们。它比拆分列表更快,但将列表移动到数组、对数组进行排序以及从排序后的数组创建新列表仍然更快。

标签: algorithm sorting recursion merge mergesort


【解决方案1】:

这是错误的:

typedef struct CELL *LIST;
struct CELL {
    int element;
    LIST next;
}

试试这个:

typedef struct CELL {
    int element;
    struct CELL *next;
} CELL, *LIST;

注意next 必须是一个指针,而不是另一个CELL 结构。

您的第一条错误消息只是指出您在声明struct CELL 后错过了分号。 (这也是为什么 merge() 的声明被忽略的原因。)

【讨论】:

  • 哦!我错过了分号。听起来差不多。感谢您的帮助!
猜你喜欢
  • 2013-08-04
  • 2021-06-27
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多