-
Notifications
You must be signed in to change notification settings - Fork 0
/
2chaining_with_replacement.cpp
186 lines (170 loc) · 6.4 KB
/
2chaining_with_replacement.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*Consider the student database of N students and their marks. Make use of a hash table
implementation to quickly insert and lookup students' PNR and marks. Implement collision
handling techniques:
1. linear probing
2. linear probing with chaining with replacement
3. Implement delete operation in collision handling using linear probing
*/
#include <iostream>
#include <vector>
using namespace std;
const int table_size = 10;
struct Student{
int pnr;
int marks;
};
class Hashtable{
public:
vector<pair<int, int>> table[table_size];
int hashfunction(int pnr){
return pnr % table_size;
}
void insertlp(int pnr, int marks){
int index = hashfunction(pnr);
while(!table[index].empty()){
index = (index + 1) % table_size;
}
table[index].push_back({pnr, marks});
cout<<"Student inserted successfully"<<endl;
}
void deletelp(int pnr){
int index = hashfunction(pnr);
while(!table[index].empty()){
for(auto it = table[index].begin(); it != table[index].end(); ++it){
if(it->first == pnr){
table[index].erase(it);
cout<<"Student with PNR "<<pnr<<" deleted successfully"<<endl;
return;
}
}
index = (index + 1) % table_size;
}
cout<<"Student with PNR "<<pnr<<" not found"<<endl;
}
void insertlpchainwithrep(int pnr, int marks){
int index = hashfunction(pnr);
if(!table[index].empty()){
int temppnr = table[index][0].first;
int tempmarks = table[index][0].second;
if(hashfunction(temppnr)==index){
table[index].push_back({pnr, marks});
}
else{
table[index][0].first = pnr;
table[index][0].second = marks;
while (!table[index].empty())
{
index = (index+1) % table_size;
}
table[index].push_back({temppnr, tempmarks});
}
}
else
{
table[index].push_back({pnr, marks});
}
cout<<"student inserted successfully"<<endl;
}
int search(int pnr){
int index = hashfunction(pnr);
while(!table[index].empty()){
for(auto student : table[index]){
if(student.first == pnr){
return student.second;
}
}
index = (index + 1) % table_size;
}
return -1;
}
void display(){
for(int i = 0; i < table_size; i++){
cout<< "Bucket "<<i<<" : ";
for(auto it : table[i]){
cout<<" ( "<<it.first<<" , "<<it.second<<" ) ";
}
cout<<endl;
}
}
};
int main(){
Hashtable t;
int choice;
int pnr;
int marks;
do {
cout<<"\nMenu : \n";
cout<<"1 Insert using linear probing\n";
cout<<"2 Insert using Linear Probing with Chaining with Replacement\n";
cout<<"3 Search a student\n";
cout<<"4 Delete a student\n";
cout<<"5 Display table\n";
cout<<"6 Exit\n";
cout<<"Enter choice : ";
cin>>choice;
switch(choice){
case 1:
{cout<<"Enter PNR and marks : ";
cin>>pnr>>marks;
t.insertlp(pnr, marks);
break;}
case 2:
{cout<<"Enter PNR and marks : ";
cin>>pnr>>marks;
t.insertlpchainwithrep(pnr, marks);
break;}
case 3:
{cout<<"Enter PNR to search : ";
cin>>pnr;
int found = t.search(pnr);
if(found == -1){
cout<<"No record found"<<endl;
}
else{
cout<<"Marks of student with PNR "<<pnr <<" is "<<found<<endl;
}
break;}
case 4:
{cout<<"Enter PNR to delete : ";
cin>>pnr;
t.deletelp(pnr);
break;}
case 5:
{cout<<"HASHTABLE : "<<endl;
t.display();
break;}
case 6:
{cout<<"Exiting..."<<endl;
break;}
default:
cout<<"Enter a valid choice"<<endl;
}
}while(choice != 6);
return 0;
}
/*THere's the algorithm for the provided C++ program implementing a hash table with different collision handling techniques:
1. **Include necessary header files**:
- Include `<iostream>` and `<vector>` for standard input/output and vector operations respectively.
2. **Define the `Student` structure**:
- Define a structure named `Student` with fields for PNR (Personal Identification Number) and marks.
3. **Define the `Hashtable` class**:
- Define a class named `Hashtable`.
- Declare a vector of pairs `table[]` to represent the hash table with a fixed size `table_size`.
- Implement a hash function `hashfunction()` to compute the index for a given PNR.
- Implement the following member functions:
- `insertlp()`: Insert a student record using linear probing.
- `deletelp()`: Delete a student record using linear probing.
- `insertlpchainwithrep()`: Insert a student record using linear probing with chaining with replacement.
- `search()`: Search for a student's marks given their PNR.
- `display()`: Display the contents of the hash table.
4. **Implement the `main()` function**:
- Create an instance of the `Hashtable` class.
- Display a menu-driven interface to perform the following operations:
- Insert a student record using linear probing.
- Insert a student record using linear probing with chaining with replacement.
- Search for a student's marks.
- Delete a student record.
- Display the contents of the hash table.
- Exit the program.
5. **End**.
This algorithm outlines the steps to implement a hash table with collision handling techniques such as linear probing and linear probing with chaining with replacement, along with functionalities to insert, search, delete, and display student records in the hash table..*/