-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithout_replacement.cpp
More file actions
148 lines (128 loc) · 3.39 KB
/
Copy pathwithout_replacement.cpp
File metadata and controls
148 lines (128 loc) · 3.39 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
#include <iostream>
#include <string.h>
#define max 10
using namespace std;
struct node {
char name[15];
long int mobno;
int chain;
node() {
strcpy(name, "-");
mobno = 0;
chain = -1;
}
};
class hasht {
node ht[max];
public:
int hashfun(long int);
void insert();
void display();
void search();
void del();
};
int hasht::hashfun(long int num) {
return (num % max);
}
void hasht::insert() {
node S;
cout << "Enter name and mobile number of a person:\n";
cin >> S.name >> S.mobno;
int ind = hashfun(S.mobno);
if (ht[ind].mobno == 0) {
ht[ind] = S;
} else {
int prev = ind;
while (ht[ind].mobno != 0) {
ind = (ind + 1) % max;
}
ht[ind] = S;
// Maintain chain from prev to current
while (ht[prev].chain != -1) {
prev = ht[prev].chain;
}
ht[prev].chain = ind;
}
}
void hasht::display() {
cout << "\nIndex\tName\t\tMobile Number\tChain\n";
for (int i = 0; i < max; i++) {
cout << i << "\t" << ht[i].name << "\t\t" << ht[i].mobno << "\t\t" << ht[i].chain << endl;
}
}
void hasht::search() {
long int num;
cout << "Enter the mobile number to search: ";
cin >> num;
int ind = hashfun(num);
while (ind != -1) {
if (ht[ind].mobno == num) {
cout << "Mobile number found at index: " << ind << endl;
cout << "Name: " << ht[ind].name << ", Mobile: " << ht[ind].mobno << endl;
return;
}
ind = ht[ind].chain;
}
cout << "Mobile number not found.\n";
}
void hasht::del() {
long int num;
cout << "Enter the mobile number to delete: ";
cin >> num;
int ind = hashfun(num), prev = -1;
while (ind != -1) {
if (ht[ind].mobno == num) {
// Case: End of chain
if (ht[ind].chain == -1) {
strcpy(ht[ind].name, "-");
ht[ind].mobno = 0;
if (prev != -1)
ht[prev].chain = -1;
cout << "Record deleted.\n";
return;
}
// Case: Has chain - move next into current
int next = ht[ind].chain;
ht[ind] = ht[next];
strcpy(ht[next].name, "-");
ht[next].mobno = 0;
ht[next].chain = -1;
cout << "Record deleted.\n";
return;
}
prev = ind;
ind = ht[ind].chain;
}
cout << "Record not found.\n";
}
int main() {
int cho;
hasht h;
char a;
do {
cout << "\nMenu:\n1. Insert\n2. Display\n3. Search\n4. Delete\n5. Exit\nEnter your choice: ";
cin >> cho;
switch (cho) {
case 1:
h.insert();
break;
case 2:
h.display();
break;
case 3:
h.search();
break;
case 4:
h.del();
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
cout << "Do you want to continue? (y/n): ";
cin >> a;
} while (a == 'y' || a == 'Y');
return 0;
}