-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.c
219 lines (188 loc) · 4.28 KB
/
trie.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
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
// In God we trust
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct node
{
char name[40];
struct node *next[10];
}
node;
node *append(node *ptr)
{
// Assigns a new node to a pointer
node *new = malloc(sizeof(node));
for (int i = 0; i < 10; i++)
{
new->next[i] = NULL;
}
strcpy(new->name, "");
// Checks whether the given pointer is NULL or not
if (ptr == NULL)
{
ptr = new;
return ptr;
}
else
{
return ptr;
}
}
node *delete(node *ptr, char date[4], int i)
{
// deletes a member of trie
node *tmp = ptr;
int place = date[i] - 48;
if (ptr == NULL)
{
return NULL;
}
// checks if the given pointer is NULL
if (tmp->next[place] == NULL)
{
return NULL;
}
// checks if the requested data is reached
if (i == 3)
{
free(tmp->next[place]);
tmp->next[place] = NULL;
}
else
{
tmp->next[place] = delete(tmp->next[place], date, i + 1);
}
if (tmp->next[place] == NULL)
{
for (int j = 0; j < 10; j++)
{
if (tmp->next[j] != NULL)
{
return tmp;
}
}
free(tmp);
return NULL;
}
return tmp;
}
int main(void)
{
char word[40];
char date[5];
node *trie = NULL;
trie = append(trie);
node *tmp = trie;
printf("Trie\n");
printf("Please enter the name of person and year of birth in four digits(type \"end\" to finish input)\n");
while (1)
{
printf("Word: ");
scanf("%s", word);
if (strcmp(word, "end") == 0)
{
break;
}
while (1)
{
// Gets and handles the errors with it
printf("Date: ");
scanf("%s", date);
if (strcmp(date, "end") == 0)
{
break;
}
if (!isdigit(date[0]) && !isdigit(date[1]) && !isdigit(date[2]) && !isdigit(date[3]))
{
printf("Please enter a number!\n");
continue;
}
else
{
break;
}
}
if (strcmp(date, "end") == 0)
{
break;
}
tmp = trie;
for (int i = 0; i < 4; i++)
{
// Adds the name to trie
int place = date[i] - 48;
tmp->next[place] = append(tmp->next[place]);
tmp = tmp->next[place];
}
strcpy(tmp->name, word);
}
printf("Please enter the date you want to delete!\n");
while (1)
{
printf("Date: ") ;
scanf("%s", date);
if (strcmp(date,"end") == 0)
{
break;
}
trie = delete(trie, date, 0);
}
printf("Now enter the Date to search(type \"end\" to finish search)\n");
while (1)
{
char date[5];
while (1)
{
// Gets and handles the errors with an int
printf("Date: ");
scanf("%s", date);
if (strcmp(date, "end") == 0)
{
break;
}
if (!isdigit(date[0]) && !isdigit(date[1]) && !isdigit(date[2]) && !isdigit(date[3]))
{
printf("Please enter a number!\n");
continue;
}
else
{
break;
}
}
if (strcmp(date, "end") == 0)
{
break;
}
// searches and prints the result
tmp = trie;
char result[40];
// checks if the pointer is NULL
if (tmp == NULL)
{
printf("Not found!\n");
continue;
}
for (int i = 0; i < 4; i++)
{
int place = date[i] - 48;
if (tmp->next[place] == NULL)
{
printf("Not found!\n");
break;
}
tmp = tmp->next[place];
}
if (tmp->name == NULL)
{
printf("Not found!\n");
continue;
}
else
{
strcpy(result, tmp->name);
printf("Result: %s\n", result);
}
}
}