【问题标题】:How can I do a recursive print using class linked list如何使用类链表进行递归打印
【发布时间】: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


【解决方案1】:

我将假设您的列表节点没有 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;
}

【讨论】:

  • 但是函数 DisplayEmployeeInReverseOrder() 会放在类之外吗?我应该在哪里编写该函数的标头和原型?
  • 如果你愿意,你可以让它成为你的类的方法,但它不需要,因为它不访问类的任何成员。将其作为类方法调用会浪费更多的堆栈空间(每次调用都会一遍又一遍地将相同的this 指针推送到堆栈上),并且在递归方面堆栈空间的使用很重要。您可以完全按照我的说明使用它,无需单独制作原型。
  • 非常感谢!!!我还有另一个小错误,它使程序每次执行时都会崩溃。当我尝试从空列表中删除时。我希望程序显示 cout
  • void EmployeeList::Remove() { newEmployee * nextToEnd = head, * last = head->Next(); //这是问题 //没有节点 if(head == NULL) return; //删除列表中唯一的员工 if(head->Next()== NULL) { cout empID() ySalary()
  • 不要将代码放入 cmets。你应该有edited your question 来显示代码,或者更好的posted a new question 关于它,因为它与你的递归问题无关。在任何情况下,如果列表为空,head 将为 NULL,因此head-&gt;Next() 崩溃。如果head 为NULL 则立即退出,不要费心初始化last。顺便说一句,您有内存泄漏,因为您在调用delete head 之前将head 设置为NULL,因此该节点实际上并未被销毁。删除 NULL 指针是无操作的。
猜你喜欢
  • 2013-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 2017-03-31
  • 1970-01-01
相关资源
最近更新 更多