solved1 thx
hashing
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 187 188 189 190 191 192 193 194 195 196 197
|
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cmath>
#include "lhash.h"
using namespace std;
lhash::lhash()
{
for(int i = 0; i< tablesize; i++)
{
hashtable[i] = new item;
hashtable[i]->name = "empty";
hashtable[i]->drink = "empty";
hashtable[i]->next = NULL;
}
}
void lhash::additem(string name, string drink)
{
int index= ash(name);
if(hashtable[index]->name == "empty")
{
hashtable[index]->name = name;
hashtable[index]->drink = drink;
}
else
{
item* ptr = hashtable[index];
item* n = new item;
n->name = name;
n->drink = drink;
n->next = NULL;
while(ptr->next != NULL)
{
ptr= ptr->next;
}
ptr->next = n;
}
}
int lhash::numitemsinindex(int index)
{
int count= 0;
if(hashtable[index]->name == "empty")
{
return count;
}
else
{
count++;
item* ptr = hashtable[index];
while(ptr->next != NULL)
{
count++;
ptr= ptr->next;
}
}
return count;
}
void lhash::printtable()
{
int number;
for(int i = 0; i< tablesize ; i++)
{
number = numitemsinindex(i);
cout<<"------------------\n";
cout<<"index = " << i << endl;
cout<<hashtable[i]->name<<endl;
cout<<hashtable[i]->drink<< endl;
cout<< "# of items = "<< number<< endl;
cout<<"------------------\n";
}
}
void lhash::printitemsinindex(int index)
{
item* ptr = hashtable[index];
if(ptr-> name == "empty")
{
cout<< "index = "<< index<< " is empty"<< endl;
}
else
{
cout<< "index "<<index<< " contains the following item\n";
while(ptr != NULL)
{
cout<<"=================\n";
cout<< ptr->name <<endl;
cout<< ptr->drink << endl;
cout<<"=================\n";
ptr = ptr->next;
}
}
}
int lhash::ash(string key)
{
int hash =0;
int index;
// adding askii value of each letter of ur umput
for(int i=0; i< key.length(); i++)
{
hash= hash + (int)key[i];
//cout<< "hash = "<< hash << endl;
}
index = hash % tablesize;
return index;
}
//
// CuckooHashTable.h
// CuckooHashTable
//
#include "CuckooHashTable.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
CuckooHashTable::CuckooHashTable()
{
// Initialize hastable
for (int i=0; i<2; i++)
{
vector<string> new_table;
for (int j=0; j<LOGICAL_SIZE; j++)
new_table.push_back("-");
contents.push_back(new_table);
}
currentSize = 0;
}
int CuckooHashTable::hashCode(string value, int which)
{
// Compute hash function for input 'value', table 'which'
if (which == 1)
return atoi(value.c_str()) % 13;
else if (which == 2)
return 11 - (atoi(value.c_str()) % 11);
else
return LOGICAL_SIZE;
}
void CuckooHashTable::add(string value)
{
// Insert 'value' to hash table
int hash_type = 1;
int index = hashCode(value, hash_type);
string start_value = value;
int start_index = index;
int start_hasht = hash_type;
do {
value.swap(contents[hash_type-1][index]);
hash_type = 3 - hash_type;
index = hashCode(value, hash_type);
if (value == start_value &&
index == start_index &&
hash_type == start_hasht){
cout << "ERROR: Inserting " + value + " causes an infinite loop.\n";
return;
}
}
while (value != "-");
this->currentSize++;
}
void CuckooHashTable::print()
{
// Print the contents of the table.
for (int i=0; i<2; i++)
{
cout << "Table " << i+1 << endl;
for (int j=0; j<LOGICAL_SIZE; j++)
cout << contents[i][j] << endl;
cout << endl;
}
}
|
Topic archived. No new replies allowed.