我將文本文件中的信息添加到節點中,然後創建一個鏈接列表,然後將其打印出來,但出現問題。我打印這些節點,結果非常完美,但是當我將它添加到列表中並打印出列表時,我得到了不斷的重複,並且需要大約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;
}
}
請查詢可以回答的問題。 – 2015-01-09 19:39:48