删除C ++中的尾递归 [英] Removal of tail recursion in C++

查看:60
本文介绍了删除C ++中的尾递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决这个问题吗?我正在尝试从
的一个小例程中删除尾递归 一些古老的代码

Could somebody help me out with this problem please? I am trying to remove tail recursion in a small routine from
some ancient code

static void new_tristrip(polygon_node **tn, edge_node *edge,
                         double x, double y)
{ polygon_node *temptn,*Stemp;

  #if 0
/* this part is the code i want to work */
    MALLOC(temptn, sizeof(polygon_node), "tristrip node creation", polygon_node);
    (temptn)->next= NULL;
    (temptn)->v[LEFT]= NULL;
    (temptn)->v[RIGHT]= NULL;
    (temptn)->active= 1;
    add_vertex(&(temptn->v[LEFT]), x, y);
    edge->outp[ABOVE]= temptn;
   if(!(*tn))
      tn=&temptn;
   else
     { Stemp=(*tn)->next;
       while((Stemp->next))Stemp=Stemp->next;
        Stemp->next=temptn;
     }
    return;

  #else
/* this is the tail recursive code */
  if (!(*tn))
  {
    MALLOC(*tn, sizeof(polygon_node), "tristrip node creation", polygon_node);
    (*tn)->next= NULL;
    (*tn)->v[LEFT]= NULL;
    (*tn)->v[RIGHT]= NULL;
    (*tn)->active= 1;
    add_vertex(&((*tn)->v[LEFT]), x, y);
    edge->outp[ABOVE]= *tn;
  }
  else
    /* Head further down the list */
    new_tristrip(&((*tn)->next), edge, x, y);
  #endif
}

推荐答案

为什么不简单:

Why not simply:

while (*tn)
    tn = &((*tn)->next);

MALLOC (*tn, ...


这篇关于删除C ++中的尾递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆