본문 바로가기

Programming/DS SorceCode

HashChainMap.h : 해시 체이닝으로 구현된 해시 맵

#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