如何使用类链表进行递归打印
How can I do a recursive print using class linked list
//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 << "nntt***THERE IS NO EMPLOYEE INFORMATION STORED YET***n";
return;
}
cout << "nn"
<< "tt************************************************n"
<< "tt* Employee IDs and Yearly Salary DataBase *n"
<< "tt************************************************nn";
cout << "tttEmployee IDs" << setw(20) << right << "Yearly Salariesn";
// One employee in the list
if(tmp->Next() == NULL )
{
cout << "ttt " << tmp->empID() << setw(13) << right << " "
<< "$" << setw(2) << tmp->ySalary() << endl;
}
else
{
do
{
cout << "ttt " << tmp->empID() << setw(13) << " "
<< right << "$" << setw(2) << tmp->ySalary() << endl;
tmp = tmp->Next();
}while(tmp != NULL );
cout << "nttt ***Thank You***" << endl;
}
}
我需要关于编写什么的帮助,以便对显示函数进行递归函数调用。我需要以从最后一个到第一个的相反顺序显示列表。如何使用类链表进行递归打印?
我将假设您的列表节点没有Previous()
方法(否则,不使用递归实现反向打印循环将变得微不足道)。
尝试这样的事情:
void DisplayEmployeeInReverseOrder(newEmployee * emp)
{
if (emp->Next() != NULL)
DisplayEmployeeInReverseOrder(emp->Next());
cout << "ttt " << emp->empID() << setw(13) << right << " "
<< "$" << setw(2) << emp->ySalary() << endl;
}
void EmployeeList::Display()
{
// Temporary pointer
newEmployee * tmp;
tmp = head;
// No employees in the list
if(tmp == NULL )
{
cout << "nntt***THERE IS NO EMPLOYEE INFORMATION STORED YET***n";
return;
}
cout << "nn"
<< "tt************************************************n"
<< "tt* Employee IDs and Yearly Salary DataBase *n"
<< "tt************************************************nn";
cout << "tttEmployee IDs" << setw(20) << right << "Yearly Salariesn";
DisplayEmployeeInReverseOrder(tmp);
cout << "nttt ***Thank You***" << endl;
}
相关文章:
- 如何使用递归打印修改后的星号三角形图案
- 使用递归 c++ 打印模式
- 使用类进行实现时,递归打印C++中的链表
- 如何使用递归打印最长公共子序列中涉及的字符串?
- 使用递归打印所有递增子序列
- 二叉搜索树按级别递归C++打印节点
- 使用递归打印输出
- 如何使用递归打印文本反驳
- 使用while和if递归打印字符
- 将数组传递到空隙函数,然后递归打印元素
- 在 C++ 中使用递归打印斐波那契级数
- 如何使用字符串流C++递归打印出二进制搜索树中的所有节点
- IntStack的C++递归打印函数
- 使用C++第2部分递归打印LinkedList
- 使用C++递归打印LinkedList
- C++如何递归打印阶乘程序的输出
- 如何使用类链表进行递归打印
- 使用斐波那契递归打印 1 到 n
- 使用单个参数递归打印菱形
- 使用递归C++打印等边三角形A-Z