
InsertValue, 값 검색 함수 이름은 FindValue로 정한다.e.g) 키를 5개 입력하세요.
11 22 54 396 87
검색할 키를 입력하세요.
22
검색되었습니다.
#include <stdio.h>
#include <string.h>
int hashtable[10][1];
//키 값에 따른 위치값 출력 함수
int Hash(int nKey) {
return nKey % 10;
}
void main()
{
int key = 22;
memset(hashtable, 0, sizeof(hashtable));
int bucket = Hash(key);
hashtable[bucket][0] = key;
}
#include <stdio.h>
#include <string.h>
#define BK 10
#define SL 1
int hashtable[BK][SL]; // 해시 테이블을 저장할 배열, 크기는 10으로 설정
// 키 값에 따른 위치값을 계산하여 반환하는 해시 함수
int Hash(int nKey) {
return nKey % 10; // 키 값에 10을 나눈 나머지를 반환하여 해시값 생성
}
// 해시 테이블에 값을 삽입하는 함수
void InsertValue(int nKey) {
int bucket = Hash(nKey); // 키 값을 해시 함수로 계산하여 버킷(위치) 찾기
if (hashtable[bucket][0] == 0) { // 해당 위치가 비어 있으면 값을 삽입
hashtable[bucket][0] = nKey;
}
}
// 해시 테이블에서 값을 찾는 함수
int FindValue(int nKey) {
int bucket = Hash(nKey); // 키 값을 해시 함수로 계산하여 버킷(위치) 찾기
return (hashtable[bucket][0] == nKey); // 해당 위치에 값이 일치하면 1을 반환, 아니면 0을 반환
}
int main() {
int key = 0; // 검색할 키 값
memset(hashtable, 0, sizeof(hashtable)); // 해시 테이블 초기화 (0으로 채움)
// 사용자로부터 5개의 키 값을 입력받아 해시 테이블에 삽입
for (int i = 0; i < 5; i++) {
printf("%d번째 값을 입력하세요 : ", i + 1);
scanf("%d", &key);
InsertValue(key); // 입력된 값을 해시 테이블에 삽입
}
// 검색할 키 값을 입력받고 검색 결과 출력
printf("검색할 키를 입력하세요 : ");
scanf("%d", &key);
if (FindValue(key)) { // 키가 테이블에 있으면
printf("검색되었습니다.\n");
} else { // 키가 테이블에 없으면
printf("검색되지 않았습니다.\n");
}
return 0;
}
hashtable[10][1] 사용.Hash)Hash(nKey) = nKey % 10 → 0~9 사이의 인덱스를 만듦.InsertValue)FindValue)memset)const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const BK = 10;
const SL = 1;
// 해시 테이블 생성
let hashtable = Array.from({ length: BK }, () => []);
// 해시 함수
function Hash(nKey) {
return nKey % 10;
}
// 값 삽입 함수
function InsertValue(nKey) {
const bucket = Hash(nKey);
if (!hashtable[bucket].includes(nKey)) {
hashtable[bucket].push(nKey);
}
}
// 값 찾기 함수
function FindValue(nKey) {
const bucket = Hash(nKey);
return hashtable[bucket].includes(nKey);
}
// 사용자 입력 받기
let inputCount = 0;
let keys = [];
function askQuestion() {
if(inputCount < 5) {
rl.question(`${inputCount + 1}번째 값을 입력하세요: `, (key) => {
InsertValue(parseInt(key, 10));
inputCount++;
askQuestion();
});
} else {
rl.question('검색할 키를 입력하세요: ', (key) => {
if (FindValue(parseInt(key, 10))) {
console.log("검색되었습니다.")
} else {
console.log('검색되지 않았습니다.')
}
rl.close();
});
}
}
askQuestion();
Array.from()let hashtable = Array.from({ length: BK }, () => []);
Array.prototype.includes()if (!hashtable[bucket].includes(nKey)) { ... }
Array.prototype.push()hashtable[bucket].push(nKey);
슬롯을 배열로 만들기
#include <stdio.h>
#include <string.h>
#define BUCKET_SIZE 10
#define SLOT_SIZE 10
int hashtable[BUCKET_SIZE][SLOT_SIZE]; // 각 해시 버킷마다 SLOT_SIZE 크기의 배열
int count[BUCKET_SIZE]; // 각 버킷에 들어간 값의 개수 (삽입 위치 추적)
int Hash(int nKey) {
return nKey % 10;
}
void InsertValue(int nKey) {
int bucket = Hash(nKey);
for (int i = 0; i < count[bucket]; i++) {
if(hashtable[bucket][i] == nKey) return;
}
if (count[bucket] < SLOT_SIZE) {
hashtable[bucket][count[bucket]] = nKey;
count[bucket]++;
}
}
int FindValue(int nKey)
{
int bucket = Hash(nKey);
for (int i = 0; i < count[bucket]; i++) {
if (hashtable[bucket][i] == nKey) {
return 1;
}
}
return 0;
}
int main() {
int key;
memset(hashtable, 0, sizeof(hashtable));
memset(count, 0, sizeof(count));
for(int i = 0; i < 5; i++) {
printf("%d번째 값을 입력하세요 : ", i + 1);
scanf("%d", &key);
InsertValue(key);
}
printf("검색할 키를 입력하세요 : ");
scanf("%d", &key);
if (FindValue(key)) {
printf("검색되었습니다.\n");
} else {
printf("검색되지 않았습니다.\n");
}
return 0;
}
버킷마다 여러 개의 값을 저장할 수 있도록, 각 버킷에 정수 배열(리스트처럼)을 연결하는 구조로 만들기.
InsertValue)void InsertValue(int nKey) {
int bucket = Hash(nKey);
// 버킷이 아직 할당되지 않았다면, 처음 할당
if (hashtable[bucket] == NULL) {
hashtable[bucket] = (int*)malloc(sizeof(int));
hashtable[bucket][0] = nKey;
count[bucket] = 1;
return;
}
// 중복 검사
for (int i = 0; i < count[bucket]; i++) {
if (hashtable[bucket][i] == nKey) return;
}
// 공간 확장
hashtable[bucket] = (int*)realloc(hashtable[bucket], sizeof(int) * (count[bucket] + 1));
hashtable[bucket][count[bucket]] = nKey;
count[bucket]++;
}
FindValue)int FindValue(int nKey) {
int bucket = Hash(nKey);
for (int i = 0; i < count[bucket]; i++) {
if (hashtable[bucket][i] == nKey) return 1;
}
return 0;
}
for (int i = 0; i < BUCKET_SIZE; i++) {
if (hashtable[i] != NULL) {
free(hashtable[i]);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUCKET_SIZE 10
int* hashtable[BUCKET_SIZE]; // 각 버킷이 가리키는 int 배열 (동적 할당)
int count[BUCKET_SIZE]; // 각 버킷에 저장된 값 개수
// 해시 함수: 키를 버킷 인덱스로 변환
int Hash(int nKey) {
return nKey % BUCKET_SIZE;
}
// 해시 테이블에 값 삽입
void InsertValue(int nKey) {
int bucket = Hash(nKey);
// 버킷이 비어 있다면 새 배열 할당
if (hashtable[bucket] == NULL) {
hashtable[bucket] = (int*)malloc(sizeof(int));
hashtable[bucket][0] = nKey;
count[bucket] = 1;
return;
}
// 중복 검사
for (int i = 0; i < count[bucket]; i++) {
if (hashtable[bucket][i] == nKey) return; // 중복이면 삽입 안 함
}
// 배열 크기 확장 후 삽입
hashtable[bucket] = (int*)realloc(hashtable[bucket], sizeof(int) * (count[bucket] + 1));
hashtable[bucket][count[bucket]] = nKey;
count[bucket]++;
}
// 해시 테이블에서 값 검색
int FindValue(int nKey) {
int bucket = Hash(nKey);
for (int i = 0; i < count[bucket]; i++) {
if (hashtable[bucket][i] == nKey) return 1; // 찾음
}
return 0; // 없음
}
// 전체 해시 테이블 출력 (디버깅용)
void PrintHashTable() {
for (int i = 0; i < BUCKET_SIZE; i++) {
printf("[%d]: ", i);
for (int j = 0; j < count[i]; j++) {
printf("%d ", hashtable[i][j]);
}
printf("\n");
}
}
int main() {
int key;
// 초기화
for (int i = 0; i < BUCKET_SIZE; i++) {
hashtable[i] = NULL;
count[i] = 0;
}
// 값 삽입
for (int i = 0; i < 5; i++) {
printf("%d번째 값을 입력하세요: ", i + 1);
scanf("%d", &key);
InsertValue(key);
}
// 해시 테이블 출력
printf("\n=== 해시 테이블 상태 ===\n");
PrintHashTable();
// 검색
printf("\n검색할 키를 입력하세요: ");
scanf("%d", &key);
if (FindValue(key)) {
printf("검색되었습니다.\n");
} else {
printf("검색되지 않았습니다.\n");
}
// 메모리 해제
for (int i = 0; i < BUCKET_SIZE; i++) {
if (hashtable[i] != NULL) {
free(hashtable[i]);
}
}
return 0;
}