给定一个节点的名称,该函数应该搜索链表;如果在内部找到,则返回一个指向该节点的指针,否则返回null。注意:我确信我已经成功地编写了这个函数。
// getNode
Node *LinkedList::getNode(string name)
{
Node *temp = head;
while (temp != NULL)
{
if (temp->name.compare(name) == 0)
return temp;
temp = temp->next;
}
return NULL;
}
给定一个节点,此函数会在屏幕上打印:teamName(winScore loseScore(。例如:加州大学洛杉矶分校(25-13(或德克萨斯A&M(31-25(。注意:我确信我已经成功地编写了这个函数。
// printNode
void LinkedList::printNode(Node *node)
{
if (node == NULL)
return;
else {
cout << node->name << "(" << node->winScore;
cout << "-" << node->loseScore << ")";
}
}
给定一个团队名称,该函数应该以以下格式逐个打印其邻接列表中的所有节点(注意:以下只是一个示例!(这就是我认为我错的地方。
Missouri University beat: New Mexico(52-23), Salisbury (48-31), Virginia (34-9)
void LinkedList::printList(string name)
{
if (head == NULL)
cout << "n Empty list" << endl;
else {
Node *temp = head;
while (temp != NULL)
{
cout << temp->name << " beat: " << temp->name << endl; // Is this right?
temp = temp->next;
}
}
}
我猜这接近于您想要的:
void LinkedList::printList(string name)
{
// find the node for the name you supply
// (or else I don't understand why 'name' is supplied to this function)
Node *temp = getNode(name);
if (temp) { // node with name found
if (temp->next) { // there's at least one adjacent node
cout << temp->name << " beat: ";
while ((temp = temp->next) != nullptr) {
printNode(temp);
if (temp->next) cout << ", ";
};
cout << "n";
} else { // no adjacent nodes
cout << temp->name << " did not beat anyonen";
}
}
}