本题要求实现一个函数,Length_LinkList(LinkList L)函数是求出带头结点单链表的长度。
函数接口定义:
int Length_LinkList(LinkList L);
其中 L
是用户传入的参数。 L
是单链表的头指针。函数须返回单链表的长度。
裁判测试程序样例:
#define FLAG -1
#include <stdio.h>
#include <malloc.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}LNode, *LinkList;
LinkList Creat_LinkList();/*这里忽略函数的实现*/
int Length_LinkList(LinkList L);
int main()
{
LinkList L;
L = Creat_LinkList();
if(L == NULL)
{
printf("L=NULL,error!");
return 0;
}
printf("%d",Length_LinkList(L) );
return 0;
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
1 2 3 4 5 6 7 8 9 10 -1
输出样例:
在这里给出相应的输出。例如:
10
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
int Length_LinkList(LinkList L){
int count = 0;
while(L->next != NULL)
{
count++;
L = L->next;
}
return count;
}