【发布时间】:2015-01-09 19:36:09
【问题描述】:
我正在将文本文件中的信息添加到节点中,然后创建一个链接列表,然后将其打印出来,但我遇到了问题。我打印了节点,结果很完美,但是当我将它添加到列表并打印出列表时,我会不断重复,并且需要大约 6 个小时才能完成列表,而它最多需要 20 秒,它最终遍历列表中的信息,但在继续之前重复某些信息大约 500 次,同时重复所有先前的信息相同的次数。这是我的add 和print 函数:
void customerlist::add(customer* ustomer)
{
customer* p = new customer;
p = ustomer;
p->next = NULL;
if (head != 0)
{
curr = head;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = p;
n++;
}
else
{
head = p;
}
}
int customerlist::getLength()
{
return n;
}
void customerlist::print()
{
curr = head;
while (curr != NULL)
{
cout << curr->ID << " ";
cout << curr->name << " " << curr->lastname << " " << curr->town << endl;
curr = curr->next;
}
}
我的主要:
while (!in.eof())
{
account* bank = new account;
customer* ustomer; in >> ustomer->ID;
// display number of customers
if (ustomer->ID < 90000000)
{
count++;
in >> ustomer->name >> ustomer->lastname >> ustomer->town;
// cout << ustomer->ID<< " " << ustomer->name << " " << ustomer->lastname<< " " << ustomer->town << endl ;
ustomerlist.add(ustomer);
ustomerlist.print();
}
else
{
break;
}
}
【问题讨论】:
-
请提出一个可以回答的问题。
标签: c++ visual-c++ visual-studio-2012 linked-list nodes