-
Notifications
You must be signed in to change notification settings - Fork 0
/
293.c
113 lines (96 loc) · 2.11 KB
/
293.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// http://www.bjfuacm.com/problem/293/
#include <stdio.h>
#include <stdlib.h>
struct list {
struct list *next;
int val;
};
void insert(struct list ***p, int e) {
struct list *q;
struct list *t; // traversal
q = (struct list *)calloc(1, sizeof(*q));
q->val = e;
q->next = NULL;
int pos = e % 13;
if ((*p)[pos] == NULL) {
(*p)[pos] = q;
} else {
t = (*p)[pos];
while (t->next != NULL) {
t = t->next;
}
t->next = q;
}
}
void create(struct list ***p, int n) {
int e;
int pos;
for (int i = 0; i < n; i++) {
scanf("%d", &e);
insert(p, e);
}
}
void clean(struct list ***p) {
struct list *t = NULL;
struct list *to_delete = NULL;
for (int i = 0; i < 13; i++) {
if ((*p)[i] == NULL) continue;
t = (*p)[i];
(*p)[i] = NULL;
while (t != NULL) {
to_delete = t;
t = t->next;
free(to_delete);
}
}
}
void delete(struct list ***p, int e) {
int pos = e % 13;
struct list *t = (*p)[pos];
if (t->val == e) {
(*p)[pos] = t->next;
free(t);
return;
} else {
struct list *pre = t;
t = t->next;
while (t != NULL) {
if (t->val == e) {
pre->next = t->next;
free(t);
return;
} else {
pre = t;
t = t->next;
}
}
}
}
void print(struct list **p) {
struct list *t;
for (int i = 0; i < 13; i++) {
if (p[i] == NULL) printf("%d\n", i);
else {
printf("%d ", i);
t = p[i];
while (t != NULL) {
printf("%d%s", t->val, t->next == NULL? "\n": " ");
t = t->next;
}
}
}
}
int main() {
struct list (**p) = (struct list **)calloc(13, sizeof(*p));
int n;
int key;
while (1) {
clean(&p);
scanf("%d", &n);
if (n == 0) break;
create(&p, n);
scanf("%d", &key);
delete(&p, key);
print(p);
}
}