C++ 基本C++;链表节点&;指针探针
很长一段时间以来,我一直无法从链表中的第一个节点检索数据。请原谅,但我对C++仍然是新手,尤其是指针和链表。 下面是我如何从文本文件中读取数据(目前工作正常)C++ 基本C++;链表节点&;指针探针,c++,pointers,linked-list,C++,Pointers,Linked List,很长一段时间以来,我一直无法从链表中的第一个节点检索数据。请原谅,但我对C++仍然是新手,尤其是指针和链表。 下面是我如何从文本文件中读取数据(目前工作正常) void readFile(ifstream&budgetFile,budgetItem*newNode,int&counter,budgetItem*temp,budgetItem*header) { char pauseChar; int-ctype; 字符串cname; 双卡蒙特; 炭清除; itemPtr listTop=NULL
void readFile(ifstream&budgetFile,budgetItem*newNode,int&counter,budgetItem*temp,budgetItem*header)
{
char pauseChar;
int-ctype;
字符串cname;
双卡蒙特;
炭清除;
itemPtr listTop=NULL;
而(!budgetFile.eof())
{
//newNode=newbudgetitem;
预算文件>>ctype>>cname>>camount>>已清除;
新建节点->类型=ctype;
cout theType next=列表顶部;
listTop=newNode;
如果(计数器==0)
{
header=newNode;
}
计数器++;
}
返回;
}
下面是我如何从第一个节点开始检索数据。这根本不起作用。在此方面的任何帮助都将不胜感激
void showBudget (budgetItem *newNode, budgetItem *temp, budgetItem *header)
{
double incomeTotal;
double expenseTotal;
double differenceTotal;
//itemPtr *newlist;
//newNode = header;
itemPtr listTop;
budgetItem *here = listTop;
do {
cout << "INCOME:" << endl;
cout << " Item Amount Cleared" << endl;
cout << " ---------------- ------- -------" << endl;
if (newNode == NULL)
{
cout << "List is empty." << endl;
}
else
{
if (newNode->theType != 0)
{
cout << " " << newNode->name << setw(23) << fixed << setprecision(2) << newNode->amount << " ";
if (newNode->cleared == true) {
cout << "no" << endl;
}
else{
cout << "yes" << endl;
}
}
else{
cout << endl;
}
}
newNode = newNode->next;
} while (newNode != NULL);
cout << "End of List." << endl;
return;
}
void showBudget(budgetItem*新节点、budgetItem*临时、budgetItem*标题)
{
双内切面;
双倍支出总额;
双差总和;
//itemPtr*新列表;
//newNode=header;
itemptrlisttop;
budgetItem*此处=列表顶部;
做{
库特
对于列表中的每个新元素,我们需要在内存中分配新对象。您一次又一次地使用同一个对象。您需要在循环的每个迭代中创建一个新节点,就像您在out commented语句中所做的那样。这段代码有很多地方出错。正如@Cheersandhth.-Alf指出的,主要是,您没有创建新的nodes,您正在覆盖单个节点。更一般地说,您在测试任何节点之前编写了大量代码,并且未能将功能解耦。从一个更简单的节点类开始,测试所有内容,然后逐步构建。
void showBudget (budgetItem *newNode, budgetItem *temp, budgetItem *header)
{
double incomeTotal;
double expenseTotal;
double differenceTotal;
//itemPtr *newlist;
//newNode = header;
itemPtr listTop;
budgetItem *here = listTop;
do {
cout << "INCOME:" << endl;
cout << " Item Amount Cleared" << endl;
cout << " ---------------- ------- -------" << endl;
if (newNode == NULL)
{
cout << "List is empty." << endl;
}
else
{
if (newNode->theType != 0)
{
cout << " " << newNode->name << setw(23) << fixed << setprecision(2) << newNode->amount << " ";
if (newNode->cleared == true) {
cout << "no" << endl;
}
else{
cout << "yes" << endl;
}
}
else{
cout << endl;
}
}
newNode = newNode->next;
} while (newNode != NULL);
cout << "End of List." << endl;
return;
}
// ...
counter++;
// create next node here:
newNode = new budgetItem;
}