해시 테이블은 키-값 쌍을 저장하며, 키를 통한 데이터 접근이 빠른 자료 구조입니다.
Keywords: #해시테이블 #자료구조 #키값
배열과 해시 함수를 사용하여 구현. 충돌 시, 연결 리스트나 오픈 어드레싱 등으로 해결.
파이썬:
hash_table = {}
C:
#include <stdio.h>
#define TABLE_SIZE 100
typedef struct {
char* key;
char* value;
} HashEntry;
HashEntry hashTable[TABLE_SIZE];
파이썬:
def insert(key, value):
hash_table[key] = value
C:
void insert(char* key, char* value) {
int hashValue = hashFunction(key);
hashTable[hashValue].key = key;
hashTable[hashValue].value = value;
}
파이썬:
def get(key):
return hash_table.get(key, "Key not found!")
C:
char* get(char* key) {
int hashValue = hashFunction(key);
if (hashTable[hashValue].key == NULL) {
return "Key not found!";
}
return hashTable[hashValue].value;
}
파이썬:
def delete(key):
if key in hash_table:
del hash_table[key]
else:
print("Key not found!")
C:
void delete(char* key) {
int hashValue = hashFunction(key);
if (hashTable[hashValue].key == NULL) {
printf("Key not found!\n");
return;
}
hashTable[hashValue].key = NULL;
hashTable[hashValue].value = NULL;
}
해시 테이블의 주된 장점은 키를 통한 빠른 접근이 가능하지만, 충돌과 해시 함수의 선택에 따라 성능이 영향을 받을 수 있습니다.