如果这是一个愚蠢/简单的问题,很抱歉..但我很迷茫。我在运行这个程序时遇到问题。我写了这个程序来读入两个值,第一个是链表中元素的数量,第二个是可以放入每个元素的最大随机值。
然后,它应该使用包含的合并排序算法对排序后的列表进行排序和重新打印。
好的,我得到的错误如下:
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;
}
发布于 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似乎实际上已经阅读了代码。
发布于 2011-11-08 10:13:50
我想你路过的所有地方
LIST merge ( LIST list1 , LIST list2 )
它应该是
LIST* merge ( LIST* list1 , LIST* list2 )
https://stackoverflow.com/questions/8045183
复制相似问题