/*查找单链表pos位置的节点,返回节点指针*/
/*pos从0开始,0返回head节点*/
node *search_node(node *head , int pos)
{
node *p = head->next;/*新建节点指针并指向头节点的下个节点*/
if(pos < 0) /*pos位置不正确*/
{
printf("incorrect position to search node!\n");
return NULL;
}
if(pos == 0) /*在head位置,返回head*/
{
return head;
}
if(p == NULL)
{
printf("Link is empty!\n"); /*链表为空*/
return NULL;
}
while(--pos)
{
if((p = p->next) == NULL)
{
printf("incorrrect position to search node !\n");
break; /*超出链表返回*/
}
}
return p;
}
本文介绍了一种用于查找单链表中特定位置节点的算法实现。该算法通过输入节点位置参数pos,返回对应节点的指针。若位置超出链表范围或链表为空,则返回错误提示。
333

被折叠的 条评论
为什么被折叠?



