【PTA】6-3 求链式表的表长 (10分)

【PTA】6-3 求链式表的表长 (10分)

函数接口定义:

int Length( List L );

其中  List结构定义如下:

typedef struct LNode *PtrToLNode; 
struct LNode {
  ElementType Data;
  PtrToLNode Next;
};
typedef PtrToLNode List;

L是给定单链表,函数Length要返回链式表的长度。

裁判测试程序样例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef int ElementType;
 5 typedef struct LNode *PtrToLNode;
 6 struct LNode {
 7     ElementType Data;
 8     PtrToLNode Next;
 9 };
10 typedef PtrToLNode List;
11 
12 List Read(); /* 细节在此不表 */
13 
14 int Length( List L );
15 
16 int main()
17 {
18     List L = Read();
19     printf("%d\n", Length(L));
20     return 0;
21 }
22 
23 /* 你的代码将被嵌在这里 */

输入样例:

1 3 4 5 2 -1

输出样例:

5

函数实现细节:

1 int Length( List L ){
2     if(L==NULL)return 0;
3     int i=0;
4     for(;L;L=L->Next)i++;
5     return i;
6 }
posted @ 2020-06-09 16:21  wyjgr  阅读(298)  评论(0编辑  收藏  举报