问题描述
我不明白......我的代码没有打印任何东西。我需要创建一个不同的节点还是我写错了代码?请帮忙。一个解释将不胜感激。代码只能到它说“从这里开始 // 到这里结束。外面的任何东西都不能修改。
预期答案:
one->eins
two->zwei
three->drei
four->vier
five->funf
one
three
zwei
funf
one->1
two->2
three->3
four->4
five->5
one
three
2
5
代码:
#include <iostream>
using namespace std;
//The class my_pair which stores two values of different types referred to as value1 and value2
//Your code starts here
template <class T,class U>
//Your code ends here
class my_pair
{
//Your code starts here
T value1;
U value2;
//Your code ends here
public:
//Your code starts here
my_pair(T value1,U value2)
{
value1 = this->value1;
value2 = this->value2;
}
//Your code ends here
T get_value1()
{
return this->value1;
}
U get_value2()
{
return this->value2;
}
};
//The class node implements a linked list of pairs
template <class T,class U>
class node
{
my_pair<T,U>* value;
node* next;
public:
//Creates a new node
node(T value1,U value2)
{
//Your code starts here
this->value = new my_pair<T,U>(value1,value2);
this->next = nullptr;
//Your code ends here
}
//Add a new pair at the end of the list
void add(T value1,U value2)
{
//Your code starts here
if (this->next == nullptr) this->next = new node(value1,value2);
this->next = nullptr;
//Your code ends here
}
//Find the value2 corresponding to the value1 passed as parameter
T find_value1(U value2)
{
//Your code starts here
node* curr = this;
while (curr != nullptr)
{
if (curr->value->get_value2() == value2)
return curr->value->get_value1();
curr->next = nullptr;
}
return 0;
//Your code ends here
}
//Find the value1 corresponding to the value2 passed as parameter
U find_value2(T value1)
{
//Your code starts here
node* current = this;
while (current != nullptr)
{
if (current->value->get_value1() == value1)
return current->value->get_value2();
current->next = nullptr;
}
return 0;
//Your code ends here
}
void print()
{
node* curr = this;
while (curr != nullptr)
{
cout << curr->value->get_value1() << "->" << curr->value->get_value2() << endl;
curr = curr->next;
}
}
};
//After
int main()
{
node<int,string>* dictionary = new node<int,string>(1,"one");
dictionary->add(2,"two");
dictionary->add(3,"three");
dictionary->add(4,"four");
dictionary->print();
cout << endl;
cout << dictionary->find_value1("one") << endl;
cout << dictionary->find_value2(4) << endl;
cout << endl;
cin.get();
node<string,string>* dictionary2 = new node<string,string>("uno","one");
dictionary2->add("dos","two");
dictionary2->add("tres","three");
dictionary2->add("cuatro","four");
dictionary2->print();
cout << endl;
cout << dictionary2->find_value2("uno") << endl;
cout << dictionary2->find_value1("cuatro") << endl;
cout << endl;
cin.get();
}
解决方法
注意事项:
-
每当您调用
add
时,您都将nullptr
分配给next
,撤消了函数第一行所做的操作。 -
由于主代码在同一个(第一个)节点上调用
add
,而不是新添加的节点,因此当前的add
例程(如果删除nullptr
赋值) 最终只会在您的列表中出现 2 个节点。您必须找到一种方法来遍历列表并找到最后一个节点,然后将其添加到该节点中。 -
您的
find_valueN
陷入无限循环。只要current
存在,它就会不断重复,但您没有任何改变current
的东西。您确实有一行更改了current->next
,但这对您没有帮助。 (实际上,这会破坏您正在查看的数据。当您只应该查看数据时,请小心修改数据。)相反,您应该做的是更改该行以使 {{1} } 成为current
中保存的值。不要更改current->next
内的任何内容。查看current
函数底部的示例。
否则看起来还好。模板定义做得很好。 :-)