单链表节点的查找
单链表节点的查找
查找单链表pos位置的节点,返回节点指针
pos从0开始,0返回head节点
node *search_node(node *head,int pos)
{
node *p=head->next;
if(pos<0)
{
printf("incorrect position to search node!\n");
return NULL;
}
if(pos==0)
{
return head;
}
if(p==NULL)
{
printf("Link is empty\n");
return NULL;
}
while(--pos)
{
if((p=p->next)==NULL)
{
printf("incorrect position to search node\n");
break;
}
}
return p;
}
查找单链表pos位置的节点,返回节点指针
pos从0开始,0返回head节点
node *search_node(node *head,int pos)
{
node *p=head->next;
if(pos<0)
{
printf("incorrect position to search node!\n");
return NULL;
}
if(pos==0)
{
return head;
}
if(p==NULL)
{
printf("Link is empty\n");
return NULL;
}
while(--pos)
{
if((p=p->next)==NULL)
{
printf("incorrect position to search node\n");
break;
}
}
return p;
}