【发布时间】:2016-03-08 02:39:02
【问题描述】:
//PROTOYPE
void Display();
//CALL
list.Display();
/***********************************
* Print the contents of the list *
***********************************/
void EmployeeList::Display()
{
// Temporary pointer
newEmployee * tmp;
tmp = head;
// No employees in the list
if(tmp == NULL )
{
cout << "\n\n\t\t***THERE IS NO EMPLOYEE INFORMATION STORED YET***\n";
return;
}
cout << "\n\n"
<< "\t\t************************************************\n"
<< "\t\t* Employee IDs and Yearly Salary DataBase *\n"
<< "\t\t************************************************\n\n";
cout << "\t\t\tEmployee IDs" << setw(20) << right << "Yearly Salaries\n";
// One employee in the list
if(tmp->Next() == NULL )
{
cout << "\t\t\t " << tmp->empID() << setw(13) << right << " "
<< "$" << setw(2) << tmp->ySalary() << endl;
}
else
{
do
{
cout << "\t\t\t " << tmp->empID() << setw(13) << " "
<< right << "$" << setw(2) << tmp->ySalary() << endl;
tmp = tmp->Next();
}while(tmp != NULL );
cout << "\n\t\t\t ***Thank You***" << endl;
}
}
为了对 Display 函数进行递归函数调用,我需要有关编写内容的帮助。 我需要从最后一个到第一个以相反的顺序显示列表。 如何使用类链表进行递归打印?
【问题讨论】:
-
开始:请不要大喊大叫。请提供minimal reproducible example。
-
为什么需要使用递归函数调用?一个简单的循环就足够了,你已经拥有了。至于以相反的顺序打印,您的节点是否有
Previous()方法来促进这一点?否则,只需将节点指针复制到一个向后排序的新列表,然后打印它。 -
程序中要求使用递归调用。教授在要求中如此指定。
标签: c++ linked-list abstract-class