P2
P2
: 12
//Assignment No. : 02
#include <iostream>
#include <vector>
class Hashing {
struct Node {
long key;
string value;
vector<Node> table;
int size;
int count;
public:
Hashing(int size) : size(size), count(0) {
table.resize(size);
}
void display() {
for (int i = 0; i < size; i++) {
if (table[i].key == -1) {
cout << "Index " << i << ": EMPTY" << endl;
} else if (table[i].key == -2) {
cout << "Index " << i << ": DELETED" << endl;
} else {
cout << "Index " << i << ": (" << table[i].key << ", " <<
table[i].value << ")" << endl;
}
}
}
};
int main() {
int n;
cout << "Enter the size of the hash table: ";
cin >> n;
Hashing hashTable(n);
int choice, num, key;
string value;
do{
cout << "***********************************" << endl;
cout << "\n Menu" << endl;
cout<<endl<<"1. Insert \n2. Search \n3. Delete \n4. Display \n5.
Exit"<<endl;
cout << "***********************************" << endl;
cout<<"Enter your Choice :";
cin>>choice;
switch(choice)
{
case 1:
cout<<"How many keys have to insert? :";
cin>>num;
for(int i=0;i<num;i++)
{
cout<<"Enter the key:";
cin>>key;
cout<<"Enter the value:";
cin>>value;
hashTable.insert(key,value);
}
break;
case 2:
cout<<"How many keys have to search? :";
cin>>num;
for(int i=0;i<num;i++)
{
cout<<"Enter the key:";
cin>>key;
hashTable.search(key);
}
break;
case 3:
cout<<"How many keys have to delete? :";
cin>>num;
for(int i=0;i<num;i++)
{
cout<<"Enter the key: ";
cin>>key;
hashTable.remove(key);
}
break;
case 4:
hashTable.display();
break;
case 5:
cout<<"Exiting..."<<endl;
break;
default:
cout<<"Wrong Input, Try Again"<<endl;
}
}while(choice!=5);
return 0;
}
/* Output
Menu
1. Insert
2. Search
3. Delete
4. Display
5. Exit
***********************************
Enter your Choice :1
How many keys have to insert? :6
Enter the key:5
Enter the value:John
Enter the key:1
Enter the value:Tom
Enter the key:10
Enter the value:James
Enter the key:26
Enter the value:Tina
Enter the key:99
Enter the value:Sana
Enter the key:105
Enter the value:Mary
Collision at index 5, probing...
Collision at index 6, probing...
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :2
How many keys have to search? :1
Enter the key:5
Key 5 found with value: John at index: 5
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :4
Index 0: (10, James)
Index 1: (1, Tom)
Index 2: EMPTY
Index 3: EMPTY
Index 4: EMPTY
Index 5: (5, John)
Index 6: (26, Tina)
Index 7: (105, Mary)
Index 8: EMPTY
Index 9: (99, Sana)
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :3
How many keys have to delete? :1
Enter the key: 5
Key 5 removed from index 5
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :4
Index 0: (10, James)
Index 1: (1, Tom)
Index 2: EMPTY
Index 3: EMPTY
Index 4: EMPTY
Index 5: DELETED
Index 6: (26, Tina)
Index 7: (105, Mary)
Index 8: EMPTY
Index 9: (99, Sana)
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :5
Exiting...
*/