道歉,如果这是一个愚蠢/简单的问题..但我很丢失。我无法运行此程序。我编写了这个程序来读取2个值,第一个是链表中的一些元素,第二个是可以放入每个元素的最大随机值。
然后应该使用包含的合并排序算法对排序列表进行排序和重新打印。
好的,所以我收到的错误如下:
base operand of `->' has non-pointer type `LIST'
和
request for member `element' in `conductor', which is of non-aggregate type `LIST *'
...(以及其他一些人)。
是的,这是一个班级..我已经编写了程序,但我不确定我在这里做错了什么或为什么我会收到错误?任何帮助表示赞赏!谢谢
#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;
}
答案 0 :(得分:2)
base operand of
- &gt;'具有非指针类型LIST'
将->
替换为.
。您想要访问本地LIST
的成员,而不是指向对象的成员。
request for member
中的 conductor', which is of non-aggregate type LIST *
元素
这是相反的。将.
替换为->
。您想要访问指向LIST
的成员,而不是指针的成员。
为了澄清,我没有阅读代码。它太多了。但这些是解决这些特定错误的常用方法。 parapura似乎实际上已经阅读了代码。
答案 1 :(得分:1)
我想你要经过的所有地方
LIST merge ( LIST list1 , LIST list2 )
应该是
LIST* merge ( LIST* list1 , LIST* list2 )
答案 2 :(得分:1)
首先:你应该永远不要让代码变得如此庞大而出现这么多错误。您应该从小而简单开始,然后在每个阶段进行构建,测试,并且永远不会添加到不起作用的代码。
这是代码的精简开头,修复了一些错误:
#include <iostream>
using namespace std;
typedef struct LIST{
int element;
LIST *next;
};
int main(){
int i, number, max;
number = 5;
max = 100;
// 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;
cout << "element " << i << " is " << conductor->element << endl;
conductor->next = new LIST;
conductor = conductor->next;
}
conductor = root; // Forgot this, didn't you!
for(i=0; i<number-2;i++){
cout << conductor->element << ", ";
conductor = conductor->next;
}
return 0;
}
看看这个,验证它是否有效,确保您了解我所做的更改,然后您可以轻松实施split
,merge
和MergeSort
个功能和I / O(一次一个,自然地在每个阶段进行测试)。