Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define EMPTY -1
- #define DELETED -2
- template<typename T1, typename T2>
- class Data {
- T1 a;
- T2 b;
- public:
- Data() {}
- Data (T1 t1, T2 t2) {
- a = t1;
- b = t2;
- }
- T1 geta() { return a; }
- void seta(T1 t1) { a = t1; }
- T2 getb() { return b; }
- void setb(T2 t2) { b = t2; }
- bool operator == (Data<string, string> & temp){
- return (a == temp.geta() && b == temp.getb());
- }
- };
- class LinearProbing {
- Data<int, string> * hashTable;
- int currentSize;
- const static int hashTableSize = 20;
- public:
- LinearProbing() {
- currentSize = 0;
- hashTable = new Data<int, string>[hashTableSize];
- for (int i = 0; i < hashTableSize; ++i) {
- hashTable[i].seta(EMPTY);
- }
- }
- int hashFunction(int key) {
- return (key % hashTableSize);
- }
- void insert(Data<int, string> myData) {
- if (isFull()) {
- return;
- }
- int hashIndex = hashFunction(myData.geta());
- while(true) {
- if (hashTable[hashIndex].geta() == EMPTY) {
- hashTable[hashIndex] = myData;
- currentSize ++;
- break;
- }
- hashIndex = ((hashIndex + 1) % hashTableSize);
- }
- }
- void deleteKeyValue(Data<int, string> myData) {
- if (isEmpty()) {
- return;
- }
- int hashIndex = hashFunction(myData.geta());
- while(true) {
- if (hashTable[hashIndex].geta() == myData.geta() && hashTable[hashIndex].getb() == myData.getb()) {
- hashTable[hashIndex].seta(EMPTY);
- currentSize --;
- break;
- }
- hashIndex = ((hashIndex + 1) % hashTableSize);
- }
- }
- bool isFull() { return (currentSize == hashTableSize); }
- bool isEmpty() { return (currentSize == 0); }
- void display() {
- for (int i = 0; i < hashTableSize; ++i)
- {
- if (hashTable[i].geta() != EMPTY) {
- cout<<i<<" ("<<hashTable[i].geta()<<", "<<hashTable[i].getb()<<")\n";
- }
- else {
- cout<<i<<"\n";
- }
- }
- }
- };
- int main() {
- LinearProbing lp;
- pair<int, string> data[] = {make_pair(19, "Alpha"),
- make_pair(27, "Beta"),
- make_pair(36, "Gamma"),
- make_pair(10, "Delta"),
- make_pair(64, "Epsilon")};
- for (int i = 0; i < 5; ++i) {
- Data<int, string> temp(data[i].first, data[i].second);
- lp.insert(temp);
- }
- lp.display();
- return 0;
- }
Add Comment
Please, Sign In to add comment