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

6-3 find the table length of the linked table

2022-07-07 22:45:00 Qingshan's green shirt

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  */

Ideas

Create a pointer p, Used to traverse a linked list . Create a counter , For statistical length .

Specific code implementation

int Length( List L )
{
    
    List p;            // The pointer p, Notice that it's not List*!!!!!!!!
    p = L;
    int j = 0;
    while( p)
    {
    
        j++;
        p = p->Next;
    }
    return j;
    }

Some of my questions
1. At first, the compilation error is because it is always written List *p!! Still don't understand structure and typedef Usage of !

2021.10.05
21:22

原网站

版权声明
本文为[Qingshan's green shirt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130603148043.html