思路:把h往后调用,当h为0时,返回,返回的值给p,判断h->date==key,若等于把让p指向该位置,返回p。
递归方程:
h0 return NULL;
h!=0 p=FindKeyNode(h->next,key);
if(h->datekey)
p=h;
return p;
运行环境:VS2017
代码实现
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int date;
struct node*next;
}ElemSN;
//创建链表
ElemSN*GreatLink(int Date[], int n)
{
int i;
ElemSN*p, *t = 0, *h = 0;
for (i = 0;i < n;i++)
{
p = (ElemSN*)malloc(sizeof(ElemSN));
p->date = Date[i];
p->next = NULL;
if (!h)
h = t = p;
else
t = t->next = p;
}
return h;
}
//输出链表
PrintLink(ElemSN*h)
{
ElemSN*p;
for (p = h;p;p = p->next)
printf("%3d", p->date);
printf("\n");
}
ElemSN*FindKeyNode(ElemSN*h, int key)
{
ElemSN*p = NULL;
if (h)
{
p = FindKeyNode(h->next, key);
if (h->date == key)
p = h;
}
return p;
}
int main(void)
{
int a[8] = { 3,2,5,8,4,7,6,9 };
ElemSN*head, *p;
head = GreatLink(a, 8);
PrintLink(head);
p = FindKeyNode(head, 7);
printf("%3d\n", p->date);
system("pause");
}
运行结果
3 2 5 8 4 7 6 9
7
请按任意键继续. . .