给出一个节点的名字,这个函数应该搜索链表;如果在其内部找到,则返回指向该节点的指针,否则返回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)。例如:UCLA(25-13)或Texas 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;
}
}
}
答案 0 :(得分:0)
我猜这很接近您想要的:
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 anyone\n";
}
}
}
答案 1 :(得分:0)
似乎是线
cout << temp->name << " beat: " << temp->name << endl;
应该是
cout << name << " beat: " << printNode(temp) << endl;
但是,这会将每个节点打印在自己的行上,即
Missouri University beat: New Mexico(52-23)
Missouri University beat: Salisbury(48-31)
Missouri University beat: Virginia(34-9)
相反,您可能想做类似的事情
cout << name << " beat: ";
while (temp != NULL) {
printNode(temp);
if (temp -> next != NULL) { // If this is not the last node
cout << ", ";
}
temp = temp -> next;
}
cout << endl;
在不知道更多的情况下这有点投机