蔡子经数据结构1.12

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


#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;
}

void reverse(node *head)
{
node *p = head->next;
head->next = NULL;

while(p)
{
node *q = p;
p = p->next;
q->next = head->next;
head->next = q;
}
}

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;
}
reverse(head);
node *p = head->next;
while(p)
{
printf("%d ",p->val);
p = p->next;
}
puts("");
return 0;
}