首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >让mergesort在链表上工作?

让mergesort在链表上工作?
EN

Stack Overflow用户
提问于 2011-11-08 10:08:37
回答 2查看 988关注 0票数 2

如果这是一个愚蠢/简单的问题,很抱歉..但我很迷茫。我在运行这个程序时遇到问题。我写了这个程序来读入两个值,第一个是链表中元素的数量,第二个是可以放入每个元素的最大随机值。

然后,它应该使用包含的合并排序算法对排序后的列表进行排序和重新打印。

好的,我得到的错误如下:

代码语言:javascript
运行
复制
base operand of `->' has non-pointer type `LIST'

代码语言:javascript
运行
复制
request for member `element' in `conductor', which is of non-aggregate type `LIST *'

...(还有其他几个)。

是的,这是一门课..我已经写了程序,但是我不确定我在这里做错了什么,或者为什么我得到了错误?如有任何帮助,我们不胜感激!谢谢

代码语言:javascript
运行
复制
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <sys/time.h>

using namespace std;

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

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

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

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

int main(int argCount, char *argVal[])
{
    int i, number, max;
    struct timeval time1;
    struct timeval time2;

    //check for correct number of arguments
    if (argCount != 3) {
        cout << "Incorrect number of arguments" << endl;
        return 0;
    }
    // initialize read in n and max values
    number = atoi(argVal[1]);
    max = atoi(argVal[2]);

    // create list and fill with random numbers
    LIST *conductor;
    LIST *root = new LIST;
    conductor = root;

    for (i = 0; i < number; i++) {
        conductor.element = rand() % max;
        conductor.next = new LIST;
        conductor = conductor.next;
    }

    // time how long it takes to sort array using mergeSort
    gettimeofday(&time1, NULL);
    mergeSort(root);
    gettimeofday(&time2, NULL);

    // print name, sorted array, and running time
    cout << "Heather Wilson" << endl;

    conductor = root;

    for (i = 0; i < number - 2; i++) {
        cout << conductor.element << ", ";
        conductor = conductor.next;
    }

    double micro1 = time1.tv_sec * 1000000 + time1.tv_usec;
    double micro2 = time2.tv_sec * 1000000 + time2.tv_usec;

    cout << conductor.element << endl;
    cout << "Running time: " << micro2 - micro1 << " microseconds" << endl;

    return 0;
}
EN

回答 2

Stack Overflow用户

发布于 2011-11-08 10:12:28

For base operand of->‘具有非指针类型LIST'

->替换为.。您希望访问本地LIST的成员,而不是指向at对象的成员。

conductor', which is of non-aggregate type LIST *中的“request for member元素”

这是相反的。将.替换为->。您希望访问指向LIST的成员,而不是指针的成员。

为了澄清,我没有读过代码。它太多了。但这些都是解决这些特定错误的常用方法。parapura似乎实际上已经阅读了代码。

票数 2
EN

Stack Overflow用户

发布于 2011-11-08 10:13:50

我想你路过的所有地方

代码语言:javascript
运行
复制
 LIST merge ( LIST list1 , LIST list2 )

它应该是

代码语言:javascript
运行
复制
 LIST* merge ( LIST* list1 , LIST* list2 )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8045183

复制
相关文章

相似问题

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