【发布时间】:2010-02-02 15:58:31
【问题描述】:
我有以下结构:
struct cell {
int nmbr;
struct cell *p;
};
我已经从这种类型创建了一个链接结构链。每个结构都通过 *p 连接到其前身。如果我决定使用递归算法打印所有nmbrs,如下所示,如何定义停止条件?
void write(struct cell* l) {
/* The following if statement doesn't solve my problem,
but hopefully you see what I'm trying to do */
if (&l != 0x000000) {
printf("%d \t", l->nmbr);
write(l->p);
}
}
【问题讨论】:
-
为什么在这种情况下使用递归? while 循环的执行速度可能比每个项目的函数调用开销快得多。在您的情况下,性能很重要吗?
标签: c recursion struct pointers