生活随笔
收集整理的這篇文章主要介紹了
Hash-table(用除法散列法实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Hash-table(用除法散列法實現)
關鍵字只支持int型,初學者版本
用鏈表解決沖突問題
#ifndef C11LEARN_HASHDIVISION_H
#define C11LEARN_HASHDIVISION_H
#include "Chain.h"
template<typename T>
class HashDivision
{
protected:int capacity
;Chain
<T
>**array
;
public:HashDivision(int capacity
= 701);HashDivision(const HashDivision
<T
> &hashDivision
);const HashDivision
<T
> & operator =(const HashDivision
<T
> &hashDivision
);T
& operator[](int key
);const T
operator[](int key
) const;Chain
<T
>* search(int key
);bool insert(Chain
<T
>* node
);bool remove(Chain
<T
>* node
);virtual ~HashDivision();protected:virtual int hashing(int index
);void clear();void copy(const HashDivision
<T
> &hashDivision
);
};
template<typename T>
HashDivision<T
>::HashDivision(int capacity
):capacity(capacity
){array
= new Chain
<T
>* [this->capacity
];
}
template<typename T>
HashDivision
<T
>::~HashDivision(){clear();delete [] array
;array
= nullptr;
}
template<typename T>
void HashDivision<T
>::clear()
{Chain
<T
>* current
;Chain
<T
>* next
;for (int i
= 0; i
< capacity
; ++i
) {current
= array
[i
];while (current
!= nullptr){next
= current
->next
;delete current
;current
= next
;}}
}
template<typename T>
HashDivision<T
>::HashDivision(const HashDivision
<T
> &hashDivision
){capacity
= hashDivision
.capacity
;array
= new Chain
<T
>* [capacity
];copy(hashDivision
);
}
template<typename T>
const HashDivision
<T
> & HashDivision
<T
>::operator =(const HashDivision
<T
> &hashDivision
){if(this == &hashDivision
) return *this;clear();delete [] array
;capacity
= hashDivision
.capacity
;array
= new Chain
<T
>* [capacity
];copy(hashDivision
);return *this;
}
template<typename T>
void HashDivision<T
>::copy(const HashDivision
<T
> &hashDivision
){Chain
<T
>* current
;Chain
<T
>* prev
;for (int i
= 0; i
< capacity
; ++i
) {prev
= nullptr;current
= hashDivision
.array
[i
];while (current
!= nullptr){prev
= current
;current
= current
->next
;}while (prev
!= nullptr){Chain
<T
>* node
= new Chain
<T
>();node
->key
= prev
->key
;node
->value
= prev
->value
;insert(node
);prev
= prev
->prev
;}}
}
template<typename T>
T
& HashDivision
<T
>::operator[](int key
){Chain
<T
>* node
= search(key
);if(node
== nullptr){node
= new Chain
<T
>();node
->key
= key
;insert(node
);}return node
->value
;
}
template<typename T>
const T HashDivision
<T
>::operator[](int key
) const{Chain
<T
>* node
= search(key
);if(node
== nullptr){throw "no find this key";}return node
->value
;
}
template<typename T>
Chain
<T
>* HashDivision<T
>::search(int key
)
{int hash_code
= hashing(key
);Chain
<T
>* current
= array
[hash_code
];while (current
!= nullptr && current
->key
!= key
){current
=current
->next
;}return current
;
}
template<typename T>
int HashDivision<T
>::hashing(int index
){return index
% capacity
;
}
template<typename T>
bool HashDivision<T
>::insert(Chain
<T
>* node
){if(node
== nullptr) return false;int hash_code
= hashing(node
->key
);Chain
<T
>* current
= array
[hash_code
];if(current
== nullptr){array
[hash_code
] = node
;}else{node
->next
= current
;current
->prev
= node
;array
[hash_code
] = node
;}return true;
}
template<typename T>
bool HashDivision<T
>::remove(Chain
<T
>* node
){if(node
== nullptr) return false;if(node
->prev
== nullptr){int hash_code
= hashing(node
->key
);array
[hash_code
] = node
->next
;}else{node
->prev
->next
= node
->next
;if(node
->next
!= nullptr)node
->next
->prev
= node
->prev
;}delete node
;node
= nullptr;return true;
}#endif
輔助類
Chain代碼鏈接
測試代碼
HashDivision
<string
> hashDivision
;hashDivision
[2] = "hello";hashDivision
[5] = "world";cout
<<hashDivision
[2]<<endl
;cout
<<hashDivision
[5]<<endl
;
總結
以上是生活随笔為你收集整理的Hash-table(用除法散列法实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。