-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
254 lines (211 loc) · 5.7 KB
/
main.c
File metadata and controls
254 lines (211 loc) · 5.7 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
#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 __node {
Member data;
struct __node *prev;
struct __node *next;
} Dnode;
typedef struct {
Dnode *head;
Dnode *crnt;
} Dlist;
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 PrintMember(const Member *x) {
printf("%d %s", x->no, x->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 Dnode *AllocDnode(void) {
return calloc(1, sizeof(Dnode));
}
static void SetDnode(Dnode *n, const Member *x, const Dnode *prev, const Dnode *next) {
n->data = *x;
n->prev = (Dnode *) prev;
n->next = (Dnode *) next;
}
static int IsEmpty(const Dlist *list) {
return list->head->next == list->head;
}
void Initialize(Dlist *list) {
Dnode *dummyNode = AllocDnode();
list->head = list->crnt = dummyNode;
dummyNode->prev = dummyNode->next = dummyNode;
}
void PrintCurrent(const Dlist *list) {
if (IsEmpty(list))
printf("The current node does not exist.");
else
PrintMember(&list->crnt->data);
}
void PrintLnCurrent(const Dlist *list) {
PrintCurrent(list);
putchar('\n');
}
Dnode *Search(Dlist *list, const Member *x, int compare(const Member *x, const Member *y)) {
Dnode *ptr = list->head->next;
while (ptr != list->head) {
if (compare(&ptr->data, x) == 0) {
list->crnt = ptr;
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
void Print(const Dlist *list) {
if (IsEmpty(list))
puts("The node does not exist.");
else {
Dnode *ptr = list->head->next;
puts("---List---");
while (ptr != list->head) {
PrintLnMember(&ptr->data);
ptr = ptr->next;
}
}
}
void InsertAfter(Dlist *list, Dnode *p, const Member *x) {
Dnode *ptr = AllocDnode();
Dnode *nxt = p->next;
p->next = p->next->prev = ptr;
SetDnode(ptr, x, p, nxt);
list->crnt = ptr;
}
void InsertFront(Dlist *list, const Member *x) {
InsertAfter(list, list->head, x);
}
void InsertRear(Dlist *list, const Member *x) {
InsertAfter(list, list->head->prev, x);
}
void Remove(Dlist *list, Dnode *p) {
p->prev->next = p->next;
p->next->prev = p->prev;
list->crnt = p->prev;
free(p);
if (list->crnt == list->head)
list->crnt = list->head->next;
}
void RemoveFront(Dlist *list) {
if (!IsEmpty(list))
Remove(list, list->head->next);
}
void RemoveRear(Dlist *list) {
if (!IsEmpty(list))
Remove(list, list->head->prev);
}
void RemoveCurrent(Dlist *list) {
if (list->crnt != list->head)
Remove(list, list->crnt);
}
void Clear(Dlist *list) {
while (!IsEmpty(list))
RemoveFront(list);
}
void Terminate(Dlist *list) {
Clear(list);
free(list->head);
}
typedef enum {
TERMINATE, INS_FRONT, INS_REAR, RMV_FRONT, RMV_REAR, PRINT_CRNT,
RMV_CRNT, SRCH_NO, 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 No", "Search Name", "Print All", "Clear"
};
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;
Dlist list;
Initialize(&list);
do {
int n;
Member x;
Member *ptr;
switch (menu = SelectMenu()) {
case INS_FRONT:
x = ScanMember("insert front", MEMBER_NO | MEMBER_NAME);
InsertFront(&list, &x);
break;
case INS_REAR:
x = ScanMember("insert rear", MEMBER_NO | MEMBER_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_NO:
x = ScanMember("search", MEMBER_NO);
if (Search(&list, &x, MemberNoCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The no does not exist.");
break;
case SRCH_NAME:
x = ScanMember("search", MEMBER_NAME);
if (Search(&list, &x, MemberNameCmp) != NULL)
PrintLnCurrent(&list);
else
puts("The name does not exist.");
break;
case PRINT_ALL:
Print(&list);
break;
case CLEAR:
Clear(&list);
break;
default:
break;
}
} while (menu != TERMINATE);
Terminate(&list);
return 0;
}