forked from Karlina-Bytes/HashTable_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
103 lines (90 loc) · 2.61 KB
/
HashTable.cpp
File metadata and controls
103 lines (90 loc) · 2.61 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
//*****************************************************************
// HashTable.cpp
// HashTable
//
// Created by Karlina Beringer on June 18, 2014.
//
// This header file contains the Hash Table class definition.
// Hash Table array elements consist of Linked List objects.
//*****************************************************************
#include "HashTable.h"
// Constructs the empty Hash Table object.
// Array length is set to 13 by default.
HashTable::HashTable( int tableLength )
{
if (tableLength <= 0) tableLength = 13;
array = new LinkedList[ tableLength ];
length = tableLength;
}
// Returns an array location for a given item key.
int HashTable::hash( string itemKey )
{
int value = 0;
for (int i = 0; i < itemKey.length(); i++)
value += itemKey[i];
return (itemKey.length() * value) % length;
}
// Adds an item to the Hash Table.
void HashTable::insertItem( Item * newItem )
{
int index = hash( newItem -> key );
array[ index ].insertItem( newItem );
}
// Deletes an Item by key from the Hash Table.
// Returns true if the operation is successful.
bool HashTable::removeItem( string itemKey )
{
int index = hash( itemKey );
return array[ index ].removeItem( itemKey );
}
// Returns an item from the Hash Table by key.
// If the item isn't found, a null pointer is returned.
Item * HashTable::getItemByKey( string itemKey )
{
int index = hash( itemKey );
return array[index].getItem( itemKey );
}
// Display the contents of the Hash Table to console window.
void HashTable::printTable()
{
cout << "\nHash Table:\n";
for (int i = 0; i < length; i++)
{
cout << "Bucket " << i+1 << ": ";
array[i].printList();
}
}
// Prints a histogram illustrating the Item distribution.
void HashTable::printHistogram()
{
cout << "\n\nHash Table Contains ";
cout << getNumberOfItems() << " Items total\n";
for (int i = 0; i < length; i++)
{
cout << i + 1 << ":\t";
for (int j = 0; j < array[i].getLength(); j++)
cout << " X";
cout << "\n";
}
}
// Returns the number of locations in the Hash Table.
int HashTable::getLength()
{
return length;
}
// Returns the number of Items in the Hash Table.
int HashTable::getNumberOfItems()
{
int itemCount = 0;
for (int i = 0; i < length; i++)
itemCount += array[i].getLength();
return itemCount;
}
// De-allocates all memory used for the Hash Table.
HashTable::~HashTable()
{
delete [] array;
}
//*****************************************************************
// End of File
//*****************************************************************