本题要求实现一个函数,求链式表的表长。
int Length( List L );
其中List结构定义如下:
typedef struct LNode *PtrToLNode; struct LNode { ElementType Data; PtrToLNode Next; }; typedef PtrToLNode List;
L是给定单链表,函数Length要返回链式表的长度。
#include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct LNode *PtrToLNode; struct LNode { ElementType Data; PtrToLNode Next; }; typedef PtrToLNode List; List Read(); /* 细节在此不表 */ int Length( List L ); int main() { List L = Read(); printf("%d\n", Length(L)); return 0; } /* 你的代码将被嵌在这里 */
1 3 4 5 2 -1
5
code:
#include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct LNode *PtrToLNode; struct LNode { ElementType Data; PtrToLNode Next; }; typedef PtrToLNode List; List Read(); /* 细节在此不表 */ int Length( List L ); int main() { List L = Read(); printf("%d\n", Length(L)); return 0; } List Read(){ int num = 0; scanf( "%d",&num ); if( -1 == num ){ return NULL; } List list = ( List )malloc( sizeof( struct LNode ) ); List last = list; list->Data = num; list->Next = NULL; scanf( "%d",&num ); while( -1 != num ){ PtrToLNode node = ( List )malloc( sizeof( struct LNode ) ); node->Data = num; node->Next = NULL; last->Next = node; last = node; scanf( "%d",&num ); } return list; } int Length( List L ){ PtrToLNode node = L; int len = 0; if( NULL == L ){ } while( NULL != node ){ len++; node = node->Next; } return len; }
pta-数据结构与算法题目集(中文)-4-3 求链式表的表长
原文:http://my.oschina.net/iamhere/blog/517821