-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
573 lines (503 loc) · 16.6 KB
/
code.cpp
File metadata and controls
573 lines (503 loc) · 16.6 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
struct node {
char name[30];
char number[15];
char address[40];
char email[40];
struct node* left, * right, * mid;
};
struct node* searchin(struct node* root, char name[], char number[]);
void modify(struct node* ptr);
void savein(struct node* head, char fn[]);
void gosave();
void insertstackpush(struct node* temp);
void load();
struct node* pop(struct node* stack);
void display();
void print();
void save();
void savein(struct node* head, char fn[]);
struct node* input(struct node*);
void deletestackpush(struct node*);
struct node* deleteNode(struct node* root[], char key[]);
struct node* deleteNodeinside(struct node*, char key[], char number[]);
struct node* findmin(struct node* tree);
void inorder(int i);
void inorderinside(struct node*);
struct node* insertinside(struct node*, struct node*);
struct node* findmin(struct node* tree);
struct node* insertstack = NULL, * deletestack = NULL, * head[26];
int main() {
struct node* n_node, * ptr;
char ans;
char n[30], num[15];
int hash, k = 0;
for (int i = 0; i < 26; i++) {
head[i] = NULL;
}
int option;
char key[40], keyn[40];
int temp_status;
cout << "************************************************************" << endl;
cout << "*********************** PHONEBOOK ***************************" << endl;
cout << "************************************************************" << endl;
load();
do {
cout << "\nMENU\n1) INSERT\n2) PRINT\n3) SEARCH AND MODIFY\n4) DELETE A CONTACT\n5) EXIT\nENTER YOUR CHOICE: ";
cin >> option;
switch (option) {
case 1:
do {
n_node = input(n_node);
hash = int(n_node->name[0]) % 65;
insertstackpush(n_node);
head[hash] = insertinside(head[hash], n_node);
cout << "\nCONTINUE? (y/n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
break;
case 2:
print();
break;
case 3:
cout << "ENTER THE SUBSCRIBER'S NAME TO BE SEARCHED: ";
cin.ignore();
cin.getline(key, 40);
while (strlen(key) == 0) {
cout << "ENTER THE SUBSCRIBER'S NAME TO BE SEARCHED: ";
cin.getline(key, 40);
}
cout << "ENTER THE SUBSCRIBER'S NUMBER: ";
cin >> keyn;
while (key[k] != '\0') {
key[k] = toupper(key[k]);
k++;
}
hash = int(key[0]) % 65;
ptr = searchin(head[hash], key, keyn);
if (ptr != NULL) {
cout << "\n\nNAME: " << ptr->name << endl;
cout << "NUMBER: " << ptr->number << endl;
cout << "ADDRESS: " << ptr->address << endl;
cout << "E-MAIL: " << ptr->email << endl;
cout << "\n\nDo you want to modify? (y/n)\n\n";
cin >> ans;
if (ans == 'y' || ans == 'Y') {
modify(ptr);
}
}
k = 0;
break;
case 4:
cout << "ENTER THE NAME: ";
cin.ignore();
cin.getline(key, 40);
while (strlen(key) == 0) {
cout << "ENTER THE NAME: ";
cin.getline(key, 40);
}
cout << "ENTER THE NUMBER: ";
cin >> num;
while (key[k] != '\0') {
key[k] = toupper(key[k]);
k++;
}
hash = int(key[0]) % 65;
head[hash] = deleteNodeinside(head[hash], key, num);
k = 0;
break;
case 5:
break;
default:
cout << "ENTER A VALID CHOICE\n";
break;
}
} while (option != 5);
gosave();
return 0;
}
struct node* insertinside(struct node* head, struct node* new_node) {
if (head == NULL) {
new_node->left = NULL;
new_node->right = NULL;
new_node->mid = NULL;
head = new_node;
}
else {
if (strcmp(head->name, new_node->name) == 0) {
if (strcmp(head->number, new_node->number) == 0) {
cout << "\n\t\tAlready exists!!!!" << endl;
}
else {
head->mid = insertinside(head->mid, new_node);
}
}
else if (strcmp(head->name, new_node->name) > 0) {
head->left = insertinside(head->left, new_node);
}
else {
head->right = insertinside(head->right, new_node);
}
}
return head;
}
void print() {
for (int i = 0; i < 26; i++) {
if (head[i] == NULL) {
continue;
}
if (head[i] != NULL) {
inorderinside(head[i]);
}
}
cout << "\n";
}
void inorderinside(struct node* head) {
if (head != NULL) {
inorderinside(head->left);
cout << "\n\nNAME: " << head->name;
cout << "\nNUMBER: " << head->number;
cout << "\nADDRESS: " << head->address;
cout << "\nE-MAIL: " << head->email;
inorderinside(head->mid);
inorderinside(head->right);
}
}
void load() {
struct node* new__node;
int hash;
char name[30];
char number[15];
char address[40];
char email[40];
char c[40];
cout << "\n\n\n\t\tPlease wait for a moment\n";
cout << "\t\tRetrieving data from file";
char fn[] = "Directory_A.txt";
for (int pp = 0; pp < 4; pp++) {
Sleep(500);
cout << ".";
}
cout << "\n\n";
for (char i = 'A'; i <= 'Z'; ++i) {
fn[10] = i;
hash = int(i) % 65;
fstream ob(fn);
while (!ob.eof()) {
ob.getline(c, 40);
if (c[0] != '\0') {
strcpy(name, c);
ob.getline(c, 40);
strcpy(number, c);
ob.getline(c, 40);
strcpy(address, c);
ob.getline(c, 40);
strcpy(email, c);
new__node = (struct node*)malloc(sizeof(struct node));
strcpy(new__node->name, name);
strcpy(new__node->number, number);
strcpy(new__node->address, address);
strcpy(new__node->email, email);
head[hash] = insertinside(head[hash], new__node);
}
}
ob.close();
ob.open(fn, ios::trunc);
ob.close();
}
}
struct node* input(struct node* ne_node) {
char name[30];
char number[15];
char address[40];
char email[40];
int k = 0;
cin.ignore();
cout << "\nEnter the name of the subscriber: ";
cin.getline(name, 30);
while ((strlen(name) == 0)) {
cout << "\nEnter a valid name: ";
cin.getline(name, 30);
}
while (name[k] != '\0') {
name[k] = toupper(name[k]);
k++;
}
cout << "\nEnter the phone number (10 digits): ";
cin.getline(number, 15);
while (strlen(number) != 10) {
cout << "\nEnter a valid 10-digit phone number: ";
cin.getline(number, 15);
}
cout << "\nEnter the address: ";
cin.getline(address, 40);
cout << "\nEnter the E-mail: ";
cin.getline(email, 40);
ne_node = (struct node*)malloc(sizeof(struct node));
strcpy(ne_node->name, name);
strcpy(ne_node->number, number);
strcpy(ne_node->address, address);
strcpy(ne_node->email, email);
return ne_node;
}
struct node* deleteNodeinside(struct node* root, char key[], char number[]) {
struct node* temp;
char mob_num[15];
if (root == NULL) {
return root;
}
if (strcmp(root->name, key) > 0) {
root->left = deleteNodeinside(root->left, key, number);
}
else if (strcmp(root->name, key) < 0) {
root->right = deleteNodeinside(root->right, key, number);
}
else {
if (strcmp(root->number, number) == 0) {
if (root->mid != NULL) {
temp = findmin(root->mid);
strcpy(root->name, temp->mid->name);
strcpy(root->number, temp->mid->number);
strcpy(root->address, temp->mid->address);
strcpy(root->email, temp->mid->email);
deletestackpush(temp->mid);
free(temp->mid);
temp->mid = NULL;
return root;
}
}
if (strcmp(root->number, number) != 0) {
temp = root;
while (strcmp(temp->mid->number, number) != 0) {
temp = temp->mid;
}
deletestackpush(temp->mid);
free(temp->mid);
if (temp->mid->mid != NULL) {
temp->mid = temp->mid->mid;
}
else {
temp->mid = NULL;
}
return temp;
}
if (root->left == NULL) {
temp = root->right;
deletestackpush(root);
free(root);
return temp;
}
else if (root->right == NULL) {
temp = root->left;
deletestackpush(root);
free(root);
return temp;
}
temp = findmin(root->right);
strcpy(root->name, temp->name);
strcpy(root->number, temp->number);
strcpy(root->address, temp->address);
strcpy(root->email, temp->email);
root->right = deleteNodeinside(root->right, temp->name, temp->number);
deletestackpush(root);
}
return root;
}
struct node* findmin(struct node* tree) {
struct node* node1 = tree;
if (tree->mid == NULL) {
while (node1 != NULL && node1->left != NULL)
node1 = node1->left;
return node1;
}
else if (tree->mid != NULL) {
while (node1 != NULL && node1->mid->mid != NULL)
node1 = node1->mid;
return node1;
}
}
void insertstackpush(struct node* temp) {
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
strcpy(newnode->name, temp->name);
strcpy(newnode->number, temp->number);
strcpy(newnode->address, temp->address);
strcpy(newnode->email, temp->email);
newnode->left = insertstack;
insertstack = newnode;
}
void display() {
struct node* ptr;
if (insertstack == NULL)
cout << "Stack is empty";
else {
ptr = insertstack;
cout << "Stack elements are: ";
while (ptr != NULL) {
cout << ptr->name << " ";
ptr = ptr->left;
}
}
}
void deletestackpush(struct node* temp) {
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
strcpy(newnode->name, temp->name);
strcpy(newnode->number, temp->number);
strcpy(newnode->address, temp->address);
strcpy(newnode->email, temp->email);
newnode->left = deletestack;
deletestack = newnode;
}
void gosave() {
fstream ob;
char fn[] = "Directory_A.txt", c[40];
int i = 0, hash, j;
cout << "\nSAVING TO FILE";
for (int pp = 0; pp < 4; pp++) {
cout << ".";
Sleep(500);
}
cout << "\n";
while (insertstack != NULL) {
fn[10] = insertstack->name[0];
ob.open(fn, ios::app);
ob << "\n" << insertstack->name << "\n" << insertstack->number << "\n" << insertstack->address << "\n" << insertstack->email;
ob.close();
insertstack = pop(insertstack);
}
while (deletestack != NULL) {
hash = int(deletestack->name[0]) % 65;
fn[10] = deletestack->name[0];
ob.open(fn, ios::out);
savein(head[hash], fn);
ob.close();
deletestack = pop(deletestack);
}
}
struct node* pop(struct node* stack) {
struct node* temp;
if (stack != NULL) {
temp = stack;
stack = stack->left;
free(temp);
return stack;
}
return NULL;
}
void savein(struct node* head, char fn[]) {
ofstream ob;
if (head != NULL) {
ob.open(fn, ios::app);
ob << "\n" << head->name << "\n" << head->number << "\n" << head->address << "\n" << head->email;
ob.close();
savein(head->left, fn);
savein(head->right, fn);
}
}
struct node* searchin(struct node* root, char name[], char number[]) {
if (root == NULL) {
cout << "\nNO SUCH SUBSCRIPTION FOUND\n";
return root;
}
else if (strcmp(root->name, name) == 0) {
if (strcmp(root->number, number) == 0)
return root;
else
return searchin(root->mid, name, number);
}
else if (strcmp(root->name, name) < 0) {
return searchin(root->right, name, number);
}
else if (strcmp(root->name, name) > 0) {
return searchin(root->left, name, number);
}
}
void modify(struct node* ptr) {
char a;
int hash1, hash, k = 0;
struct node* ptr1 = (struct node*)malloc(sizeof(struct node));
do {
cout << "\nWhich entity has to be modified?\nN - NAME\nA - ADDRESS\nP - PHONE NUMBER\nE - EMAIL ID\n";
cin >> a;
switch (a) {
case 'N':
char name[30];
cin.ignore();
cout << "Enter the new name of the subscriber: ";
cin.getline(name, 30);
while (strlen(name) == 0) {
cout << "Enter a valid name: ";
cin.getline(name, 30);
}
while (name[k] != '\0') {
name[k] = toupper(name[k]);
k++;
}
hash1 = int(name[0]) % 65;
hash = int(ptr->name[0]) % 65;
strcpy(ptr1->name, name);
strcpy(ptr1->address, ptr->address);
strcpy(ptr1->number, ptr->number);
strcpy(ptr1->email, ptr->email);
head[hash1] = insertinside(head[hash1], ptr1);
print();
head[hash] = deleteNodeinside(head[hash], ptr->name, ptr->number);
print();
cout << "\n\nNAME: " << ptr1->name << endl;
deletestackpush(ptr1);
deletestackpush(ptr);
break;
case 'A':
char address[40];
cin.ignore();
cout << "Enter the new address of the subscriber: ";
cin.getline(address, 40);
while (strlen(address) == 0) {
cout << "Enter a valid address: ";
cin.getline(address, 40);
}
strcpy(ptr->address, address);
deletestackpush(ptr);
break;
case 'P':
char no[20];
cin.ignore();
cout << "Enter the new phone number of the subscriber: ";
cin.getline(no, 20);
while (strlen(no) != 10) {
cout << "Enter a valid 10-digit phone number: ";
cin.getline(no, 20);
}
strcpy(ptr->number, no);
deletestackpush(ptr);
break;
case 'E':
char mail[40];
cin.ignore();
cout << "Enter the new E-mail ID of the subscriber: ";
cin.getline(mail, 40);
while (strlen(mail) == 0) {
cout << "Enter a valid E-mail ID: ";
cin.getline(mail, 40);
}
strcpy(ptr->email, mail);
deletestackpush(ptr);
break;
default:
cout << "Unrecognized character entered!\nKindly re-enter a character from the list or press 'M' to cancel the MODIFY process\n";
cin >> a;
}
if (a != 'M' && a != 'm') {
cout << "Modification still working... press 'M' to terminate, and to process press 'Y'\n";
cin >> a;
}
} while (a != 'M' && a != 'm');
}