-
Notifications
You must be signed in to change notification settings - Fork 1
/
source.c
190 lines (160 loc) · 4.14 KB
/
source.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEMBER_NO 1
#define MEMBER_NAME 2
typedef struct {
int no;
char name[20];
} Member;
typedef struct __bnode {
Member data;
struct __bnode *left;
struct __bnode *right;
} BinNode;
int MemberNoCmp(const Member *x, const Member *y) {
return x->no < y->no ? -1 : x->no > y->no ? 1 : 0;
}
int MemberNameCmp(const Member *x, const Member *y) {
return strcmp(x->name, y->name);
}
void PrintLnMember(const Member *x) {
printf("%d %s\n", x->no, x->name);
}
Member ScanMember(const char *message, int sw) {
Member temp;
printf("Please input data to %s.\n", message);
if (sw & MEMBER_NO) {
printf("No: ");
scanf("%d", &temp.no);
}
if (sw & MEMBER_NAME) {
printf("Name: ");
scanf("%s", temp.name);
}
return temp;
}
static BinNode *AllocBinNode(void) {
return calloc(1, sizeof(BinNode));
}
static void SetBinNode(BinNode *n, const Member *x,
const BinNode *left, const BinNode *right) {
n->data = *x;
n->left = (BinNode *) left;
n->right = (BinNode *) right;
}
BinNode *Search(BinNode *p, const Member *x) {
int cond;
if (p == NULL)
return NULL;
else if ((cond = MemberNameCmp(x, &p->data)) == 0)
return p;
else if (cond < 0)
return Search(p->left, x);
else
return Search(p->right, x);
}
BinNode *Add(BinNode *p, const Member *x) {
int cond;
if (p == NULL) {
p = AllocBinNode();
SetBinNode(p, x, NULL, NULL);
} else if ((cond = MemberNameCmp(x, &p->data)) == 0)
printf("[ERROR] %s already exists.\n", x->name);
else if (cond < 0)
p->left = Add(p->left, x);
else
p->right = Add(p->right, x);
return p;
}
int Remove(BinNode **root, const Member *x) {
BinNode *next, *temp;
BinNode **left;
BinNode **p = root;
while (1) {
int cond;
if (*p == NULL) {
printf("[ERROR] %s does not exists.\n", x->name);
return -1;
} else if ((cond = MemberNameCmp(x, &(*p)->data)) == 0)
break;
else if (cond < 0)
p = &((*p)->left);
else
p = &((*p)->right);
}
if ((*p)->left == NULL)
next = (*p)->right;
else {
left = &((*p)->left);
while ((*left)->right != NULL)
left = &(*left)->right;
next = *left;
*left = (*left)->left;
next->left = (*p)->left;
next->right = (*p)->right;
}
temp = *p;
*p = next;
free(temp);
return 0;
}
void PrintTree(const BinNode *p) {
if (p != NULL) {
PrintTree(p->left);
PrintLnMember(&p->data);
PrintTree(p->right);
}
}
void FreeTree(BinNode *p) {
if (p != NULL) {
FreeTree(p->left);
FreeTree(p->right);
free(p);
}
}
typedef enum {
TERMINATE, ADD, REMOVE, SEARCH, PRINT_ALL
} Menu;
Menu SelectMenu(void) {
int i, ch;
char *mstring[] = {"Insert", "Remove", "Search", "Print"};
do {
for (i = TERMINATE; i < PRINT_ALL; i++) {
printf("(%2d) %-18.18s ", i + 1, mstring[i]);
if ((i % 3) == 2)
putchar('\n');
}
printf("( 0) End : ");
scanf("%d", &ch);
} while (ch < TERMINATE || ch > PRINT_ALL);
return (Menu) ch;
}
int main(void) {
Menu menu;
BinNode *root = NULL;
do {
Member x;
BinNode *temp;
switch (menu = SelectMenu()) {
case ADD:
x = ScanMember("insert", MEMBER_NO | MEMBER_NAME);
root = Add(root, &x);
break;
case REMOVE:
x = ScanMember("remove", MEMBER_NAME);
Remove(&root, &x);
break;
case SEARCH:
x = ScanMember("search", MEMBER_NAME);
if ((temp = Search(root, &x)) != NULL)
PrintLnMember(&temp->data);
break;
case PRINT_ALL:
puts("--- List ---");
PrintTree(root);
break;
}
} while (menu != TERMINATE);
return 0;
}