思路:将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请按任意键继续. . .
本文介绍了如何使用C语言通过递归方式实现非空单向链表的逆序输出。思路是先将链表节点指针置空,然后递归处理下一个节点,最后输出当前节点的数据。代码已在VS2017环境下运行成功。
&spm=1001.2101.3001.5002&articleId=102825196&d=1&t=3&u=d54118fc3bc2410ca71033fb66605bea)
1万+

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



