-
Notifications
You must be signed in to change notification settings - Fork 0
/
231.c
77 lines (69 loc) · 1.5 KB
/
231.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// http://www.bjfuacm.com/problem/231/
#include <stdio.h>
#include <stdlib.h>
struct Node;
typedef struct Node LinkedList;
typedef struct Node* PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int ElementType;
struct Node {
ElementType val;
Position next;
};
List InitList(List L, int len) {
L = (PtrToNode)calloc(1, sizeof(LinkedList));
L->next = NULL;
List prev = L;
List post;
for (int i = 0; i < len; i++) {
post = (PtrToNode)calloc(1, sizeof(LinkedList));
scanf("%d", &post->val);
post->next = NULL;
prev->next = post;
prev = post;
}
return L;
}
List Reverse(List L) {
Position cur = L->next;
free(L);
Position prev = NULL;
Position next = NULL;
while (cur != NULL) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
List NewL = (PtrToNode)calloc(1, sizeof(LinkedList));
NewL->next = prev;
return NewL;
}
void DestroyList(List L) {
Position p;
p = L->next;
L->next = NULL;
while (p != NULL) {
free(p);
p = p->next;
}
}
int main() {
List L;
int len;
while (1) {
scanf("%d", &len);
if (len == 0) break;
L = InitList(L, len);
L = Reverse(L);
Position p = L->next;
while (p) {
printf("%d", p->val);
printf("%s", p->next == NULL ? "\n" : " ");
p = p->next;
}
DestroyList(L);
free(L);
}
}