当前位置:网站首页>6-3 find the table length of the linked table

6-3 find the table length of the linked table

2022-07-05 06:36:00 timingzj

This question requests to realize a function , Find the length of the linked list .

Function interface definition :

int Length( List L );

among List The structure is defined as follows :

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

L Is a given single linked list , function Length To return the length of the linked list .

Sample referee test procedure :

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

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

List Read(); /*  Details are not shown here  */

int Length( List L );

int main()
{
    List L = Read();
    printf("%d\n", Length(L));
    return 0;
}

/*  Your code will be embedded here  */

sample input :

1 3 4 5 2 -1

sample output :

5

Code :

int Length( List L )
{
    int size = 0;
    while(L)
    {
        L = L->Next;
        size++;
    }
    return size;
}

原网站

版权声明
本文为[timingzj]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140603055318.html