本題要求實現一個函數,求鏈式表的表長。
函數接口定義:
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
 
        int Length( List L )
{
    int len = 1;
    if(L==NULL) return 0;
    while(L->Next!=NULL)
    {
        len++;
        L=L->Next;
    }
        return len;
}
 
        
  
         
         
       