-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
63 lines (52 loc) · 1.13 KB
/
HashTable.cpp
File metadata and controls
63 lines (52 loc) · 1.13 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
#include "HashTable.h"
// Constructor
HashTable::HashTable(unsigned int capacity)
{
table = new LinkedList[capacity];
capacity_ = capacity;
size_ = 0;
}
// Destructor
HashTable::~HashTable()
{
delete [] table;
table = NULL;
}
// Member Functions
bool HashTable::search(string key, Share &stock)
{
unsigned int index = hashIndex(key);
unsigned int items = table[index].size();
for (int i = 0; i < items; i++)
{
if (table[index].select(i).getTicker() == key)
{
stock = table[index].select(i);
return true;
}
}
return false;
}
void HashTable::insert(Share stock)
{
string key = stock.getTicker();
unsigned int index = hashIndex(key);
table[index].insert_front(stock);
}
void HashTable::remove(string key)
{
unsigned int index = hashIndex(key);
unsigned int items = table[index].size();
for (int i = 0; i < items; i++)
{
if (table[index].select(i).getTicker() == key)
return table[index].remove(i);
}
}
unsigned int HashTable::hashIndex(string key)
{
int index = 1;
for (int i = 0; i < key.length(); i++)
index *= key[i];
return (index % capacity_);
}