SHOW:
|
|
- or go back to the newest paste.
1 | - | struct foo |
1 | + | // primary structure |
2 | - | { |
2 | + | struct foo |
3 | - | Data data; |
3 | + | { |
4 | - | foo* next; |
4 | + | Data data; |
5 | - | foo** prevNext; |
5 | + | foo* next; |
6 | foo** prevNext; | |
7 | }; | |
8 | ||
9 | HashTable<foo> hash; | |
10 | - | template <class T> |
10 | + | |
11 | - | void HashTable<T>::insert(T* x) |
11 | + | // insert |
12 | - | { |
12 | + | template <class T> |
13 | - | int bucket = hashFunc(x->data); |
13 | + | void HashTable<T>::insert(T* x) |
14 | - | T** xPtr = &elements_[bucket]; |
14 | + | { |
15 | - | x->next = *xPtr; |
15 | + | int bucket = hashFunc(x->data); |
16 | - | x->prevNext = xPtr; |
16 | + | T** xPtr = &elements_[bucket]; |
17 | - | |
17 | + | x->next = *xPtr; |
18 | - | if (x->next) |
18 | + | x->prevNext = xPtr; |
19 | - | x->next->prevNext = &x->next; |
19 | + | |
20 | - | |
20 | + | if (x->next) |
21 | - | *xPtr = x; |
21 | + | x->next->prevNext = &x->next; |
22 | ||
23 | *xPtr = x; | |
24 | - | template<class T> |
24 | + | |
25 | - | void HashTable<T>::remove(T* x) |
25 | + | |
26 | - | { |
26 | + | // remove |
27 | - | if (x->next) |
27 | + | template<class T> |
28 | - | x->next->prevNext = x->prevNext; |
28 | + | void HashTable<T>::remove(T* x) |
29 | - | |
29 | + | { |
30 | - | *x->prevNext = x->next; |
30 | + | if (x->next) |
31 | x->next->prevNext = x->prevNext; | |
32 | ||
33 | - | template<class T> |
33 | + | *x->prevNext = x->next; |
34 | - | T* HashTable<T>::find(Data& data) |
34 | + | |
35 | - | { |
35 | + | |
36 | - | int bucket = hashFunc(data); |
36 | + | // find |
37 | - | |
37 | + | template<class T> |
38 | - | T* ptr = elements_[bucket]; |
38 | + | T* HashTable<T>::find(Data& data) |
39 | - | while (ptr != 0) |
39 | + | { |
40 | - | { |
40 | + | int bucket = hashFunc(data); |
41 | - | if(ptr->data == data) |
41 | + | |
42 | - | return ptr; |
42 | + | T* ptr = elements_[bucket]; |
43 | - | ptr = ptr->next; |
43 | + | while (ptr != 0) |
44 | - | } |
44 | + | { |
45 | - | return 0; |
45 | + | if(ptr->data == data) |
46 | return ptr; | |
47 | ptr = ptr->next; | |
48 | } | |
49 | return 0; | |
50 | } |