【发布时间】:2021-06-15 12:29:07
【问题描述】:
我想不通....我的代码没有打印任何东西。我需要创建一个不同的节点还是我编写了错误的代码?请帮忙。一个解释将不胜感激。代码只能去它说“从这里开始//从这里结束。不能修改之外的任何内容。
预期答案:
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();
}
【问题讨论】:
-
您是否在调试器中单步调试过您的代码以了解其运行方式?
-
是的,但我的输出是 0->
-
要审查的代码很多,因此您至少应该能够弄清楚它为什么会卡住。
-
在函数
void add(T value1, U value2)中,为什么添加后将next设置为nullptr?在您的find_value函数中也是如此。另一个:在您的my_pair构造函数中,值或混合(首选构造函数中的初始化列表,并避免 func 参数和类 var 的名称相同)。 -
想知道我们是否可以提前关闭函数(在
//Your code starts here//Your code ends here内部)以创建其他函数以遵守 5 规则;-)
标签: c++ visual-c++ c++14