#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct
{
DataType data;
struct Node* next;
}Node;
void InitNode(Node* first) //初始化单链表
{
first = (Node*)malloc(sizeof(Node));
first->next = NULL;
}
void CreaNode(Node* first,DataType n) //头插法创建单链表
{
Node* p;
for (int i = n; i > 0; i--)
{
p = (Node*)malloc(sizeof(Node));
p->data = i;
p->next = first->next;
first->next = p;
}
}
int ListLength(Node* first)
{
Node * p;
int j = 0;
p = first->next;
while (p!= NULL)
{
p = p->next;
j++;
}
return j;
}
int main()
{
int n;
int e;
Node E;
printf("input n:");
scanf("%d", &n);
InitNode(&E);
CreaNode(&E, n);
e = ListLength(&E);
printf("单链表的长度:%d", e);
return 0;
}