6-1 求鏈式表的表長 (10 分)
阿新 • • 發佈:2018-12-22
本題要求實現一個函式,求鏈式表的表長。
函式介面定義:
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
我的程式碼:
int Length( List L )
{
int len=1;
if(L==NULL)
return 0;
while(L->Next!=NULL)
{
len++;
L=L->Next;
}
return len;
}