思路:将h调到null然后返回,返回输出h->date。
递归方程:
h==0 return
h!=0 Printx(h->next)
out h->date;
运行环境:VS2107
代码实现
#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");
}
void Print(ElemSN*h)
{
if (h)
{
Print(h->next);
printf("%3d", h->date);
}
}
int main(void)
{
int a[8] = { 3,2,5,8,4,7,6,9 };
ElemSN*head;
head = GreatLink(a, 8);
PrintLink(head);
Print(head);
system("pause");
}
运行结果:
3 2 5 8 4 7 6 9
9 6 7 4 8 5 2 3请按任意键继续. . .