蔡子经数据结构1.11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36



#include <stdlib.h>

typedef struct
{
int val;
struct * next;
} node;

node* createNode()
{
node *ret = (node*)malloc(sizeof(node));
ret->val = 0;
ret->next = NULL;
return ret;
}

int main()
{
node *head = createNode();
node *tail = head;
for(int i = 0; i < 10; ++i)
{
node *p = createNode();
p->val = i+1;
tail->next = p;
tail = tail->next;
}
node *p = head->next;
int cnt = 0;
while(p) p = p->next, ++cnt;
printf("%dn",cnt);
return 0;
}