-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
287 lines (244 loc) · 6.9 KB
/
main.c
File metadata and controls
287 lines (244 loc) · 6.9 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PHYSCHECK_NAME 1
#define PHYSCHECK_VISION 2
#define PHYSCHECK_HEIGHT 4
typedef struct {
double vision;
int height;
} Body;
typedef struct {
Body body;
char *name;
} PhysCheck;
typedef struct __node {
PhysCheck data;
struct __node *next;
} Node;
typedef struct {
Node *head;
Node *crnt;
} List;
int PhysCheckVisionCmp(const PhysCheck *x, const PhysCheck *y) {
return x->body.vision < y->body.vision ? -1 : x->body.vision > y->body.vision ? 1 : 0;
}
int PhysCheckHeightCmp(const PhysCheck *x, const PhysCheck *y) {
return x->body.height < y->body.height ? -1 : x->body.height > y->body.height ? 1 : 0;
}
int PhysCheckNameCmp(const PhysCheck *x, const PhysCheck *y) {
return strcmp(x->name, y->name);
}
void PrintPhysCheck(const PhysCheck *x) {
printf("%0.1lf %d %s", x->body.vision, x->body.height, x->name);
}
void PrintLnPhysCheck(const PhysCheck *x) {
printf("%0.1lf %d %s\n", x->body.vision, x->body.height, x->name);
}
PhysCheck ScanPhysCheck(const char *message, int sw) {
PhysCheck temp;
printf("Please input data to %s.\n", message);
if (sw & PHYSCHECK_VISION) {
printf("vision: ");
scanf("%lf", &temp.body.vision);
}
if (sw & PHYSCHECK_HEIGHT) {
printf("height: ");
scanf("%d", &temp.body.height);
}
if (sw & PHYSCHECK_NAME) {
temp.name = calloc(82, sizeof(char));
printf("name: ");
scanf("%s", temp.name);
}
return temp;
}
static Node *AllocNode(void) {
return calloc(1, sizeof(Node));
}
static void SetNode(Node *n, const PhysCheck *x, Node *next) {
n->data = *x;
n->next = next;
}
void Initialize(List *list) {
list->head = NULL;
list->crnt = NULL;
}
Node *Search(List *list, const PhysCheck *x,
int compare(const PhysCheck *x, const PhysCheck *y)) {
Node *ptr = list->head;
while (ptr != NULL) {
if (compare(&ptr->data, x) == 0) {
list->crnt = ptr;
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
void InsertFront(List *list, const PhysCheck *x) {
Node *ptr = list->head;
list->head = list->crnt = AllocNode();
SetNode(list->head, x, ptr);
}
void InsertRear(List *list, const PhysCheck *x) {
if (list->head == NULL)
InsertFront(list, x);
else {
Node *ptr = list->head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = list->crnt = AllocNode();
SetNode(ptr->next, x, NULL);
}
}
void RemoveFront(List *list) {
if (list->head != NULL) {
Node *ptr = list->head->next;
free(list->head);
list->head = list->crnt = ptr;
}
}
void RemoveRear(List *list) {
if (list->head != NULL) {
if ((list->head)->next == NULL)
RemoveFront(list);
else {
Node *ptr = list->head;
Node *pre;
while (ptr->next != NULL) {
pre = ptr;
ptr = ptr->next;
}
pre->next = NULL;
free(ptr);
list->crnt = pre;
}
}
}
void RemoveCurrent(List *list) {
if (list->head != NULL) {
if (list->crnt == list->head)
RemoveFront(list);
else {
Node *ptr = list->head;
while (ptr->next != list->crnt)
ptr = ptr->next;
ptr->next = list->crnt->next;
free(list->crnt);
list->crnt = ptr;
}
}
}
void Clear(List *list) {
while (list->head != NULL)
RemoveFront(list);
list->crnt = NULL;
}
void PrintCurrent(const List *list) {
if (list->crnt == NULL)
printf("The current node does not exist.");
else
PrintPhysCheck(&list->crnt->data);
}
void PrintLnCurrent(const List *list) {
PrintCurrent(list);
putchar('\n');
}
void Print(const List *list) {
if (list->head == NULL)
puts("The node does not exist.");
else {
Node *ptr = list->head;
puts("---List---");
while (ptr != NULL) {
PrintLnPhysCheck(&ptr->data);
ptr = ptr->next;
}
}
}
void Terminate(List *list) {
Clear(list);
}
typedef enum {
TERMINATE, INS_FRONT, INS_REAR, RMV_FRONT, RMV_REAR, PRINT_CRNT,
RMV_CRNT, SRCH_VISION, SRCH_HEIGHT, SRCH_NAME, PRINT_ALL, CLEAR
} Menu;
Menu SelectMenu(void) {
int i, ch;
char *mstring[] = {
"Insert Front", "Insert Rear", "Remove Front",
"Remove Rear", "Print Current", "Remove Current",
"Search Vision", "Search Height", "Search Name",
"Print All", "Clear All"
};
do {
for (i = TERMINATE; i < CLEAR; 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 > CLEAR);
return (Menu) ch;
}
int main(void) {
Menu menu;
List list;
PhysCheck x;
Initialize(&list);
do {
switch (menu = SelectMenu()) {
case INS_FRONT:
x = ScanPhysCheck("insert front", PHYSCHECK_VISION | PHYSCHECK_HEIGHT | PHYSCHECK_NAME);
InsertFront(&list, &x);
break;
case INS_REAR:
x = ScanPhysCheck("insert rear", PHYSCHECK_VISION | PHYSCHECK_HEIGHT | PHYSCHECK_NAME);
InsertRear(&list, &x);
break;
case RMV_FRONT:
RemoveFront(&list);
break;
case RMV_REAR:
RemoveRear(&list);
break;
case PRINT_CRNT:
PrintLnCurrent(&list);
break;
case RMV_CRNT:
RemoveCurrent(&list);
break;
case SRCH_VISION:
x = ScanPhysCheck("search", PHYSCHECK_VISION);
if (Search(&list, &x, PhysCheckVisionCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The vision does not exist.");
break;
case SRCH_HEIGHT:
x = ScanPhysCheck("search", PHYSCHECK_HEIGHT);
if (Search(&list, &x, PhysCheckHeightCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The height does not exist");
break;
case SRCH_NAME:
x = ScanPhysCheck("search", PHYSCHECK_NAME);
if (Search(&list, &x, PhysCheckNameCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The name does not exist");
break;
case PRINT_ALL:
Print(&list);
break;
case CLEAR:
Clear(&list);
break;
}
} while (menu != TERMINATE);
Terminate(&list);
return 0;
}