计算列表中与递归链接列表中的最后一个节点具有相同数据的节点数

时间:2017-02-17 06:05:37

标签: c++ recursion

我正在练习LLL递归,而且我目前仍然坚持这个问题。以下是我到目前为止的情况:

count_last(node* head, int &last_node_data)
{
     if(!head) return 0;
     if(!head->next)
     {last = head->data;
       return 0;
     }
      count_last(head->next,last);
      if(head->data == last)
           //Increase count by 1;
  }

如何增加点数?在使用递归(从列表的末尾)向后工作时,我真的很挣扎。谢谢!

2 个答案:

答案 0 :(得分:0)

你可以:

  • 使用静态局部变量(仅限一次性使用,因为它永远不会重新初始化)
  • 使用全局变量
  • 将计数作为参数传递,返回更新后的计数。
  • 将指针作为参数传递给计数,将其更新

答案 1 :(得分:0)

尝试这样的事情:

while (true)
{
    Thread.Sleep(AutoSaveTime);
    if (isRunning)
    {
        try
        {
            Workbooks files = Globals.ThisAddIn.Application.Workbooks;
            foreach (Workbook book in files)
            {
                if (book.Saved != true 
                           && book != Globals.ThisAddIn.Application.ActiveWorkbook)
                {
                    book.Save();
                }
            }
            Globals.ThisAddIn.Application.ActiveWorkbook.Save();
        }
        catch (Exception)
        {
        }
    }
}