c - 没有 p=p->link 的链表箭头运算符循环
问题描述
如何使用多个箭头运算符(->)到达具有 x 个节点的链表的最后一个节点?
我很好奇如何在我的代码中表达 -> 操作 x 次
伪代码 p (->next)*x
我想做 p->link->link->..... 我想做 x 次而不使用 p=p->link
解决方案
你可以使用递归:
struct List
{
struct List *next;
};
struct List *findLast(struct List *node)
{
if(node -> next) return findLast(node -> next);
return node;
}
struct List *findNth(struct List *node, size_t N)
{
if(N && node -> next) return findLast(node -> next, N - 1)
if(!N) return node;
return NULL;
}
推荐阅读
- sql - SQL:Parent,Child,Subchild,仅当其所有孩子都有特定的子孩子时才选择父母
- java - ScheduledExecutorService 随机停止运行
- json - 如果多个键具有确切的名称,如何访问 JSON 值?
- linux - 为什么不自动收割子进程?
- python - 如何从数据框中获取最大值?
- azure - 更改邀请电子邮件中inviteRedeemUrl 的导航:Azure AD
- android - 当任何一侧超出网络覆盖范围时,Pjsip 呼叫不会断开连接
- html - 为什么我的页脚显示在我的页面顶部?
- r - 时间序列 - 检测季节性以设置频率
- javascript - 如何在网站上获得清晰的图像质量?