我可以打印一個字符串。但是當我嘗試添加兩個字符串時,它只打印第一個字符串? curr是鏈表的開始。如果我添加一個國家並告訴計劃打印它將打印該國家的信息。但是,如果我添加兩個國家,它將只打印第一個國家。爲什麼我不能打印多個字符串?
void LinkedList::printList()
{
curr = head;
while (curr)
{
cout << "Country Name: " << curr->name << ", " << "Gold: " << curr->
gold << ", " << "Silver: " << curr->silver << ", " << "Bronze: " << curr->bronze << "\n";
curr = curr->next;
}
}
bool LinkedList::addCountry(string newName, int gold, int silver, int bronze) {
if (head == NULL)// Adding first element
{
head = new struct Country;
if (head == NULL) return false; // could not create head linked list country
head->name = newName;
head->gold = gold;
head->silver = silver;
head->bronze = bronze;
head->next = NULL;
return true;
} else {
curr = head;
while (curr) {
curr = curr->next;
}
curr = new struct Country;
if (curr == NULL)
return false;
curr->name = newName;
curr->gold = gold;
curr->silver = silver;
curr->bronze = bronze;
curr->next = NULL;
return true;
}
}
你確定第二個正確結束在列表中? – tkausl
@AndrewL不,你不需要那個。 –
我們可以看到如何將字符串添加到鏈接列表中? –