-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashCuckoo.cpp
More file actions
111 lines (102 loc) · 2.34 KB
/
Copy pathhashCuckoo.cpp
File metadata and controls
111 lines (102 loc) · 2.34 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
#include <iostream>
#include <cmath>
#include "hashCuckoo.hpp"
using namespace std;
node* HashTable::createNode(int key){
node* nw = new node;
nw->key = key;
return nw;
}
HashTable::HashTable(int bsize){ // Constructor
this->tableSize= bsize;
table1 = new node*[tableSize];
table2 = new node*[tableSize];
for(int i=0;i<bsize;i++){
table1[i] = NULL;
table2[i] = NULL;
}
}
unsigned int HashTable::hashFunction0(int key){
return (key%10009);
}
unsigned int HashTable::hashFunction1(int key){
int y = (int) floor(key/10009);
return (y%10009);
}
int x=0;
// inserts a key into hash table
void HashTable::insertItemCuckoo(int key){
int index1 = hashFunction0(key);
if(x>=100){
x=0;
return;
}
if(table1[index1] == NULL){
table1[index1] = createNode(key);
}
else{
node *temp = table1[index1];
table1[index1] = NULL;
table1[index1] = createNode(key);
int index2 = hashFunction1(temp->key);
if(table2[index2] == NULL){
table2[index2] = createNode(temp->key);
}
else{
node *temp2 = table2[index2];
table2[index2] = NULL;
table2[index2] = createNode(temp->key);
x++;
insertItemCuckoo(table2[index2]->key);
}
}
}
void HashTable::printTable(){
cout<<"Table 1:"<<endl;
for(int i = 0; i < tableSize; i++){
cout<<i<<" || ";
node *temp = table1[i];
if(temp != NULL){
cout<<table1[i]->key;
}
cout<<endl;
}
cout<<"Table 2:"<<endl;
for(int j = 0; j < tableSize; j++){
cout<<j<<" || ";
if(table2[j] != NULL){
cout<<table2[j]->key;
}
cout<<endl;
}
}
node* HashTable::Lookup(int key){
int index1 = hashFunction0(key);
int index2 = hashFunction1(key);
if(table1[index1]->key == key){
return table1[index1];
}
else if(table2[index2]->key == key){
return table2[index2];
}
else{
cout<<"Value not found."<<endl;
return NULL;
}
}
void HashTable::Delete(int key){
int index1 = hashFunction0(key);
int index2 = hashFunction1(key);
if(table1[index1]->key == key){
delete table1[index1];
table1[index1] = NULL;
}
else if(table2[index2]->key == key){
delete table2[index2];
table2[index2] = NULL;
}
else{
cout<<"Value not found."<<endl;
return;
}
}