-
Notifications
You must be signed in to change notification settings - Fork 0
/
trueCaller.cpp
169 lines (124 loc) · 2.89 KB
/
trueCaller.cpp
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
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class Node{
public:
int value;
char *name;
Node **children;
int childCount;
Node(): value(-1), name(NULL), children(NULL), childCount(0){
}
~Node(){
for(int i=0; i< childCount; i++){
delete children[i];
}
delete [] children;
}
friend class Trie;
};
class Trie{
public:
bool insertNumber(Node *root, long int number, char*string){
if(number== 0){
return true;
}
int currentDigit= number% 10;
if(root-> children== NULL){
root-> children= new Node*[10];
root-> children[0]= new Node;
root-> children[0]-> value= currentDigit;
root-> childCount+= 1;
number/= 10;
if(insertNumber(root-> children[root-> childCount- 1], number, string)){
root-> children[root-> childCount-1]->name= new char[strlen(string)+ 1];
strcpy(root->children[(root->childCount)-1]->name, string);
}
return false;
}
for(int i=0; i< root-> childCount; i++){
if(root-> children[i]-> value== currentDigit){
number/= 10;
insertNumber((root-> children[i]), number, string);
return false;
}
}
root-> children[root-> childCount]= new Node;
root-> children[root-> childCount]-> value= currentDigit;
root-> childCount+= 1;
number/= 10;
if(insertNumber(root-> children[root-> childCount- 1], number, string)){
root-> children[root-> childCount-1]->name= new char[strlen(string)+ 1];
strcpy(root->children[(root->childCount)-1]->name, string);
}
return false;
}
void search(Node *root, long int number){
if(root-> childCount== 0){
cout<< "contact name is:";
cout<< " "<< root-> name<< endl;
}
int currentDigit= number% 10;
for(int i=0; i< root-> childCount; i++){
if(root-> children[i]-> value== currentDigit){
number/= 10;
// cout<< currentDigit;
search((root-> children[i]), number);
}
}
return ;
}
void print(Node *root){
cout<< root-> childCount;
if(root-> childCount== 0){
return ;
}
for(int i=0; i< root-> childCount; i++){
cout<< root-> children[i]->value<< " ";
}
cout<< endl;
for(int i=0; i< root-> childCount; i++){
print((root-> children[i]));
}
}
};
int main(){
Node *root;
root= new Node;
Trie t;
int q;
long int n;
char name[40];
bool stop= false;
char ch;
while(1){
cout<< "Enter your Query"<< endl;
cout<< "1) Enter new contact."<< endl;
cout<< "2) Search a contact."<< endl;
cout<< "3) Exit."<< endl;
cin>> q;
ch= cin.get();
switch(q){
case 1: cout<< "Enter the name of contact"<< endl;
cin.getline(name, 40);
cout<< "Enter the contact number"<< endl;
cin>> n;
t.insertNumber(root, n, name);
break;
case 2: cout<< "Enter the contact number"<< endl;
cin>> n;
t.search(root, n);
break;
case 3: stop= true;
break;
default: cout<<"invalid entry"<< endl;
}
if(stop){
break;
}
q=0;
}
delete root;
return 0;
}