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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
template <class elemType>
class hashT
{
public:
void insert(int hashIndex, elemType& rec);
void search(int& hashIndex, elemType& rec, bool& found) const;
bool isItemAtEqual(int hashIndex, const elemType& rec) const;
void retrieve(int hashIndex, elemType& rec) const;
void remove(int hashIndex, elemType& rec);
void print() const;
int hashFunction(string name);
hashT(int size = 101);
~hashT();
private:
elemType *HTable;
int *indexStatusList;
int length;
int HTSize;
};
template <class elemType>
void hashT<elemType>::insert(int hashIndex, elemType& rec)
{
int pCount;
int inc;
pCount = 0;
inc = 1;
while (indexStatusList[hashIndex] == 1
&& HTable[hashIndex] != rec
&& pCount < HTSize / 2)
{
cout << "inc = " << inc << endl;
pCount++;
hashIndex = (hashIndex + inc) % HTSize;
cout << "new hashIndex = " << hashIndex << endl;
inc = inc + 2;
}
if (indexStatusList[hashIndex] != 1)
{
HTable[hashIndex] = rec;
// cout << "HTable[" << hashIndex << "]" << " = " << rec << endl;
indexStatusList[hashIndex] = 1;
length++;
}
else
if (HTable[hashIndex] == rec)
cerr << "Error: No duplicates are allowed" << endl;
else
cerr << "Error: The table is full. "
<< "Unable to resolve the collision" << endl;
}
template <class elemType>
void hashT<elemType>::search(int& hashIndex, elemType& rec, bool& found)const
{
int pCount;
int inc;
pCount = 0;
inc = 1;
while (indexStatusList[hashIndex] != -1
&& HTable[hashIndex] != rec
&& pCount < HTSize / 2)
{
pCount++;
hashIndex == (hashIndex + inc) % HTSize;
inc = inc + 2;
}
if (indexStatusList[hashIndex] == 1 /*&& HTable[hashIndex] == rec*/)
{
rec = HTable[hashIndex];
found = true;
}
else
{
found = false;
}
}
template <class elemType>
void hashT<elemType>::remove(int hashIndex, elemType& rec)
{
// bool found;
if (indexStatusList[hashIndex] != 1)
{
cout << "State not found" << endl << endl;
return;
}
else
{
indexStatusList[hashIndex] = -1;
length--;
cout << "State Deleted" << endl << endl;
}
}
template <class elemType>
bool hashT<elemType>::isItemAtEqual(int hashIndex, const elemType& rec)const
{
return(HTable[hashIndex] == rec);
}
template <class elemType>
void hashT<elemType>::retrieve(int hashIndex, elemType& rec)const
{
if (indexStatusList[hashIndex] == 1)
rec = HTable[hashIndex];
}
template <class elemType>
void hashT<elemType>::print()const
{
for (int i = 0; i < HTSize; i++)
if (indexStatusList[i] != 0)
cout << i << " " << indexStatusList[i]
<< " " << HTable[i] << endl;
}
template <class elemType>
hashT<elemType>::hashT(int size)
{
assert(size > 0);
HTable = new elemType[size];
indexStatusList = new int[size];
length = 0;
HTSize = size;
for (int i = 0; i < size; i++)
indexStatusList[i] = 0;
}
template <class elemType>
hashT<elemType>::~hashT()
{
delete[] HTable;
delete[] indexStatusList;
}
class StateData
{
public:
StateData();
StateData(string, string);
void setStateInfo(string, string);
string getName();
string getCapital();
string getStateInfo();
friend bool operator < (StateData &, StateData &);
friend bool operator > (StateData &, StateData &);
bool operator != (StateData& const);
friend bool operator == (StateData &, StateData &);
friend ostream &operator << (ostream&, StateData&);
friend istream &operator >> (istream &, StateData &);
private:
string name;
string capital;
};
StateData::StateData()
{
name = " ";
capital = " ";
}
StateData::StateData(string n, string c)
{
name = n;
capital = c;
}
void StateData::setStateInfo(string n, string c)
{
name = n;
capital = c;
}
string StateData::getName()
{
return name;
}
string StateData::getCapital()
{
return capital;
}
//string StateData::getStateInfo()
//{
// return ;
//}
bool operator<(StateData &right, StateData &left)
{
return (right.getName() < left.getName());
}
bool operator>(StateData &right, StateData &left)
{
return (right.getName() > left.getName());
}
bool operator==(StateData &right, StateData &left)
{
return (right.getName() == left.getName());
}
bool StateData::operator!=(StateData &left)
{
return (this->name != left.name);
}
ostream &operator << (ostream& out, StateData&a)
{
cout << setw(18)<< left << a.getName() << a.getCapital() << endl;
return out;
}
istream &operator>>(istream &in, StateData &a)
{
string n, c;
cout << "Enter State: ";
getline(cin, n);
cout << "Enter Capital: ";
getline(cin, c);
a.setStateInfo(n, c);
return in;
}
int hashFunc(string name, int size);
int main()
{
/*int x = 0;
std::clock_t start;
double duration;
start = std::clock();*/
hashT<StateData> HashTable(60);
int size = 60;
ifstream infile;
infile.open("statesinfo.txt");
if (!infile)
{
cout << "Error reading states file.\n";
exit(1);
}
int key;
string state, capital, name;
StateData temp;
bool found;
int choice;
found = false;
getline(infile, state);
while (infile)
{
getline(infile, capital);
temp.setStateInfo(state, capital);
key = hashFunc(state, size);
HashTable.insert(key, temp);
getline(infile, state);
}
cout << setw(6)<< "\n\nINDEX " << "STATUS " << setw(18) << left << "STATE" << "CAPITAL" << endl << endl;
HashTable.print();
while (1)
{
cout << "\n------------------------------------------------------------------------" << endl;
cout << " Operations on Hash Table" << endl;
cout << "\n------------------------------------------------------------------------" << endl;
cout << "1. Insert a state into the Hash Table" << endl << endl;
cout << "2. Search for a state in the Hash Table" << endl << endl;;
cout << "3. Delete a state from the Hash Table" << endl << endl;
cout << "4. Exit" << endl << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
while (key != 1)
{
cout << "Enter the state: ";
getline(cin, state);
getline(cin, state);
cout << "Enter the capital: ";
getline(cin, capital);
key = hashFunc(state, size);
temp.setStateInfo(state, capital);
HashTable.insert(key, temp);
key = 1;
}
HashTable.print();
break;
case 2:
cout << "Enter state to search: ";
getline(cin, state);
getline(cin, state);
key = hashFunc(state, size);
temp.setStateInfo(state, capital);
HashTable.search(key, temp, found);
cout << endl;
if (found == true)
{
cout << "\nState Found: " << key << " " << temp << endl;
}
else
{
cout << "\nState not found" << endl;
}
cout << endl;
HashTable.print();
break;
case 3:
cout << "Enter state to delete: ";
getline(cin, state);
getline(cin, state);
key = hashFunc(state, size);
temp.setStateInfo(state, capital);
HashTable.remove(key, temp);
HashTable.print();
break;
case 4:
exit(1);
default:
cout << "\nThat is not a valid option. Choose either 1, 2, 3 or 4.\n";
}
}
infile.close();
return 0;
}
int hashFunc(string name, int size)
{
int i, sum, len;
i = 0;
sum = 0;
len = name.length();
for (int k = 0; k < 15 - len; k++)
name = name + ' ';
for (int k = 0; k < 5; k++)
{
sum = sum + static_cast<int>(name[i]) * 128 * 128
+ static_cast<int>(name[i + 1]) * 128
+ static_cast<int>(name[i + 2]);
i = i + 3;
}
return sum % size;
}
|