-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocabbot.c
243 lines (188 loc) · 5.56 KB
/
vocabbot.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <ctype.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char* word;
struct node* next;
struct node* prev;
} word_node;
word_node* insert_node(word_node* head_ptr, char* word);
word_node* delete_node(word_node* head_ptr, char* word);
int find_word(word_node* head_ptr, char* word);
void print_list(word_node* head_ptr);
void to_lowercase(char* word);
bool only_letters(char* word);
void free_nodes(word_node* head_ptr);
void handler_sigint(int _);
// for handling SIGINT
bool keep_running = true;
int main() {
printf("commands: -delete=WORD | -find=WORD | -quit\n");
word_node* head_ptr = NULL;
char input_string[54];
// for handling SIGINT
struct sigaction act;
act.sa_handler = handler_sigint;
sigaction(SIGINT, &act, NULL);
while (keep_running) {
printf("insert word to the list or enter command: ");
if (scanf("%s", input_string) != 1 && keep_running == true) {
printf("error: failed to get input\n");
continue;
}
to_lowercase(input_string);
if (input_string[0] == '-' && keep_running == true) {
if (strncmp(input_string, "-delete=", 8) == 0) {
head_ptr = delete_node(head_ptr, input_string + 8);
continue;
} else if (strncmp(input_string, "-find=", 6) == 0) {
find_word(head_ptr, input_string + 6);
continue;
} else if (strcmp(input_string, "-quit") == 0) {
break;
}
printf("error: invalid command\n");
continue;
}
if (keep_running == true) {
head_ptr = insert_node(head_ptr, input_string);
}
}
print_list(head_ptr);
free_nodes(head_ptr);
return 0;
}
word_node* insert_node(word_node* head_ptr, char* word) {
if (!only_letters(word)) {
printf(
"invalid input: '%s' contains non alphabetical characters\n",
word
);
return head_ptr;
}
word_node* new_node = malloc(sizeof(word_node));
new_node -> word = strdup(word);
new_node -> next = NULL;
new_node -> prev = NULL;
if (head_ptr == NULL) {
printf("word '%s' inserted as the first word\n", new_node -> word);
return new_node;
}
word_node* ptr = head_ptr;
while (true) {
int comparison = strcmp(ptr -> word, new_node -> word);
if (comparison == 0) {
printf("word '%s' is already in the list\n", word);
free(new_node -> word);
free(new_node);
return head_ptr;
}
if (comparison > 0) {
new_node -> next = ptr;
new_node -> prev = ptr -> prev;
if (ptr -> prev) {
ptr -> prev -> next = new_node;
} else {
head_ptr = new_node;
}
ptr -> prev = new_node;
printf("word '%s' inserted to the list\n", word);
return head_ptr;
}
// enter new node as the last node
if (ptr -> next == NULL) {
ptr -> next = new_node;
new_node -> prev = ptr;
printf(
"word '%s' inserted at the end of the list\n", new_node -> word
);
return head_ptr;
}
ptr = ptr -> next;
}
return head_ptr;
}
word_node* delete_node(word_node* head_ptr, char* word) {
if (head_ptr == NULL) {
printf("the list has no words\n");
return NULL;
}
word_node* ptr = head_ptr;
while (ptr != NULL) {
if (strcmp(ptr -> word, word) == 0) {
if (ptr -> prev) {
ptr -> prev -> next = ptr -> next;
} else {
head_ptr = ptr -> next;
}
if (ptr -> next) {
ptr -> next -> prev = ptr -> prev;
}
printf("word '%s' deleted from the list\n", word);
free(ptr -> word);
free(ptr);
return head_ptr;
}
ptr = ptr -> next;
}
printf("word '%s' is not in the list\n", word);
return head_ptr;
}
int find_word(word_node* head_ptr, char* word) {
if (head_ptr == NULL) {
printf("the list has no words\n");
return 0;
}
word_node* ptr = head_ptr;
while (ptr != NULL) {
if (strcmp(ptr -> word, word) == 0) {
printf("word '%s' is in the list\n", word);
return 0;
}
ptr = ptr -> next;
}
printf("word '%s' is not in the list\n", word);
return 0;
}
// visualize list (runs at SIGINT or -quit)
void print_list(word_node* head_ptr) {
if (head_ptr == NULL) {
printf("the list has no words\n");
}
word_node* ptr = head_ptr;
int count = 0;
while (ptr != NULL) {
count++;
printf("%d. %s\n", count, ptr -> word);
ptr = ptr -> next;
}
}
void to_lowercase(char* word) {
for (int i = 0, n = strlen(word); i < n; i++) {
word[i] = tolower(word[i]);
}
}
bool only_letters(char* word) {
for (int i = 0, n = strlen(word); i < n; i++) {
if (isalpha(word[i]) == 0) {
return false;
}
}
return true;
}
void free_nodes(word_node* head_ptr) {
word_node* ptr = head_ptr;
while (ptr != NULL) {
word_node* next = ptr -> next;
free(ptr -> word);
free(ptr);
ptr = next;
}
}
void handler_sigint(int _) {
printf("\nkeyboard interrupt received\nexiting application...\n");
keep_running = false;
}