6-5 求单链表结点的阶乘和 (15分)

6-5 求单链表结点的阶乘和 (15分)

#include <stdio.h>
#include <stdlib.h>

typedef struct Node *PtrToNode;
struct Node {
    int Data; /* 存储结点数据 */
    PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */
int FactorialSum( List L );
int main()
{
    int N, i;
    List L, p;

    scanf("%d", &N);
    L = NULL;
    for ( i=0; i<N; i++ ) {
        p = (List)malloc(sizeof(struct Node));
        scanf("%d", &p->Data);
        p->Next = L;  L = p;
    }
    printf("%d\n", FactorialSum(L));

    return 0;
}
/* 你的代码将被嵌在这里 */
int fun(int n)
{
	if(n==0||n==1)
	return 1;
	else
	{
		return n*fun(n-1);
	}
}
int FactorialSum( List L )
{
	int sum=0;
	while(L!=NULL)
	{
		int t=fun(L->Data);
		sum+=t;
		L=L->Next;
	}
	return sum;
}
发布了74 篇原创文章 · 获赞 0 · 访问量 1903

猜你喜欢

转载自blog.csdn.net/qq_38054511/article/details/104107970