2016-03-08 39 views
-4
//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函数执行递归函数调用。 我需要按照从倒数到倒数的顺序显示列表。 如何使用类链接列表进行递归打印?如何使用类链接列表执行递归打印

+1

首先:不喊吧。请提供[MCVE]。 –

+0

我可以听到你好 –

+0

为什么你需要使用*递归*函数调用?一个简单的循环就足够了,你已经有了。至于按相反顺序打印,你的节点是否有一个'Previous()'方法来实现这一点?否则,只需将节点指针复制到向后排序的新列表中,然后再打印。 –

回答

0

我打算假设您的列表节点没有Previous()方法(否则反向打印循环在不使用递归的情况下实现将会很微不足道)。

尝试这样:

void DisplayEmployeeInReverseOrder(newEmployee * emp) 
{ 
    if (emp->Next() != NULL) 
     DisplayEmployeeInReverseOrder(emp->Next()); 

    cout << "\t\t\t " << 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 << "\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"; 

    DisplayEmployeeInReverseOrder(tmp); 

    cout << "\n\t\t\t  ***Thank You***" << endl; 
} 
+0

但函数DisplayEmployeeInReverseOrder()将放置在类之外?我应该在哪里编写该函数的标题和原型? –

+0

如果你愿意,你可以使它成为你的课程的一种方法,但不需要,因为它没有访问课程的任何成员。它会浪费更多的堆栈空间来将其称为类方法(每次调用都会一遍又一遍地将相同的'this'指针放到堆栈上),并且在递归时堆栈空间的使用非常重要。您可以完全按照我所示的方式使用它,您无需单独对其进行原型设计。 –

+0

非常感谢你!我还有另一个小错误,使得程序每次执行时都会崩溃。这是当我尝试从空列表中删除。而不是崩溃我想电子商务程序显示cout <<“员工列表是空的”<< endl;但它会崩溃。 –