免责声明:这是作业,但我不是在找你们写我的代码。我正在寻找关于我做错了什么以及正确方向的建议。
有一个带有格式化数据的文件(时间,温度,风速)。我应该创建一个包含三个不同“链”的链表;每个链具有相同的数据,但顺序不同,使得带有时间的链将按时间顺序读取所有数据,带有temp的链将按临时顺序读取数据,带有风速的链将以风速顺序读取数据。
我编写了一个可以成功读取给定指针数据并按时间正确组织列表的函数。但现在我正在寻找使用相同的函数来读取相同的指针并组织其他2个列表。
以下是相关代码:
void datalogger::addData(int timestamp, double temperature, double windspeed){
weatherdata *n;
n = new weatherdata;
n->time = timestamp;
n->temp = temperature;
n->wind = windspeed;
n->next = nullptr;
ll.addData(n, 1);
}
void linkedlist::addData(weatherdata *newData, int selectionNo){
weatherdata *selection;
switch(selectionNo){
//linkedlist: TIME
case 1:
selection = timeHead;
break;
//linkedList: TEMP
case 2:
selection = tempHead;
break;
//linkedList: WIND
case 3:
selection = windHead;
break;
}
int marker = 0;
weatherdata *node;
weatherdata *current;
weatherdata *nextNumber;
node = new weatherdata;
node = newData;
node->next = nullptr;
//very first entry
if (selection == nullptr){
selection = node;
marker = 1;
}
//item should instead be in the first position
if (newData->time < selection->time){
nextNumber = timeHead;
selection = node;
node->next = nextNumber;
}
else {
//traverse the list until we find the correct place to put it in
current = selection;
nextNumber = selection->next;
//while "not yet at the end of the list"
while (nextNumber != nullptr){
//if its the same as another item DELETE
if ((newData->time == current->time) || (newData->time == nextNumber->time)) {
marker = 1;
break;
}
//the new item goes between the current and nextNumber
else if ((current->time < newData->time) && (newData->time < nextNumber->time)){
current->next = node;
node->next = nextNumber;
break;
}
//otherwise increment current and nextNumber for the next comparison
else {
current = nextNumber;
nextNumber = nextNumber->next;
}
}
//item goes at the end of the list
if ((nextNumber == nullptr) && (marker != 1)){
current->next = node;
}
}
switch(selectionNo){
case 1:
timeHead = selection;
break;
case 2:
tempHead = selection;
break;
case 3:
windHead = selection;
break;
}
}
基本上我想做的就是转过来:
timeHead-&GT;时间
进入这个:
whicheverLinkedListHeadIwant-&GT; whicheverClassMemberIwant
这样,我可以使用相同的函数来处理所有三个链表。您可能已经注意到,在我的linkedlist :: addData函数的开头使用switch语句,我能够获得处理“whitoverLinkedListHeadIwant”的代码,但是对于我的生活,我无法得到“whicheverClassMemberIwant”到工作
这是我尝试过的:
char* classMemberSelection = "temp";
要么
char* classMemberSelection = "wind"
whicheverLinkedListHeadIwant->classMemberSelection;
但编译器只返回一个错误,指出classMemberSelection不是成员。我不知道如何制作第二部分,无论我想要它是什么。
如果这个问题没有意义,我很抱歉。我尽力尽可能清楚,但我仍然是所有这一切的初学者。如果您需要我发布项目的其他部分,那么我很乐意帮忙。
答案 0 :(得分:0)
首先我们定义三个函数,每个函数对应一种比较
bool comparetime(weatherdata * left,
weatherdata * right)
{
return left->time < right->time;
}
bool comparetemp(weatherdata * left,
weatherdata * right)
{
return left->temp < right->temp;
}
bool comparewind(weatherdata * left,
weatherdata * right)
{
return left->wind < right->wind;
}
所有这些人都会告诉你一个人是否比另一个小。现在我们调整
void linkedlist::addData(weatherdata *newData, int selectionNo)
到
void linkedlist::addData(weatherdata *newData,
bool (*comparefp)(weatherdata * left,
weatherdata * right))
将上述函数之一作为参数。接下来,我们想要找到列表newData
中的位置并将其弹出:
while (nextNumber != nullptr && comparefp(newData, nextNumber))
// keep looking until end of list or we pass the insert location
{
lastNumber = nextNumber;
nextNumber = nextNumber.next;
}
// insert into list
newData.next=nextNumber;
lastNumber.next=newData;
并致电linkedlist::addData
void datalogger::addData(int timestamp, double temperature, double windspeed){
weatherdata *n;
n = new weatherdata;
n->time = timestamp;
n->temp = temperature;
n->wind = windspeed;
n->next = nullptr;
// add to each list
timelist.addData(n, comparetime);
templist.addData(n, comparetemp);
windlist.addData(n, comparewind);
}