#pragma once
#ifndef ___HashChainMap
#define ___HashChainMap
// HashChainMap.h : 해시 체이닝으로 구현된 해시 맵
#include "HashNode.h"
#include "hashFunctions.h"
class HashChainMap {
Node* table[TABLE_SIZE];
public:
HashChainMap() { for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL; }
void display() {
for (int i = 0; i < TABLE_SIZE; i++) {
printf("[%2d] ", i);
for (Node* p = table[i]; p != NULL; p = p->getLink())
printf("%10s", p->getKey());
printf("\n");
}
}
// 체인법을 이용하여 체이블에 키를 삽입
void addRecord(Node *n) {
int hashValue = hashFunction((n->getKey()).c_str());
for (Node* p = table[hashValue]; p != NULL; p = p->getLink()) {
if (p->equal((n->getKey()).c_str())) {
printf("이미 탐색키가 저장되어 있음\n");
delete n;
return;
}
}
n->setLink(table[hashValue]);
table[hashValue] = n;
}
// 체인법을 이용하여 테이블에 저장된 키를 탐색
void searchRecord(const char* key) {
int hashValue = hashFunction(key);
for (Node* p = table[hashValue]; p != NULL; p = p->getLink()) {
if (p->equal(key)) {
printf("탐색 성공");
p->display();
return;
}
}
printf("탐색 실패 : %s\n", key);
}
};
#endif // !___HashChainMap
'Programming > DS SorceCode' 카테고리의 다른 글
STL 맵 클래스의 활용 : 영어 단어장 (0) | 2019.04.06 |
---|---|
HashNode.h : 해시 맵을 위한 노드 클래스 (0) | 2019.04.06 |
HashMap.h : 해싱을 이용하는 맵 클래스 #오류 있음 (0) | 2019.04.06 |
HashRecord.h : 해시 맵을 위한 keyed Record 클래스 (0) | 2019.04.06 |
해싱 (0) | 2019.04.06 |