해시 표 생 성 (c 언어 분리 링크 법 / 체인 주소 법)
6720 단어 알고리즘 관련 문제
typedef enum{
HASH_OK,
HASH_ERROR,
HASH_ADDED,
HASH_REPLACED_VALUE,
HASH_ALREADY_ADDED,
HASH_DELETED,
HASH_NOT_FOUND,
} HASH_RESULT;
typedef struct __HashEntry HashEntry;
struct __HashEntry{
union{
char *str_value;
double dbl_value;
int int_value;
} key;
union{
char *str_value;
double dbl_value;
int int_value;
long long_value;
void *ptr_value;
} value;
HashEntry *next;
};
struct __HashTable{
HashEntry **bucket;
int size;
HASH_RESULT last_error;
};
typedef struct __HashTable HashTable;
// hash_size , HashTable , NULL。
HashTable *create_hash(int hash_size);
해시 표 관련 설명: 1. HASHRESULT 형식 은 관련 함수 의 반환 형식 2. HashEntry 는 해시 표 에 저 장 된 요소 (즉, 키 값 은 키, value) 유형 3. HashTable 은 해시 표 입 니 다. 그 중에서 bucket 은 크기 가 size 이 고 요소 유형 은 4. HashEntry * 인 포인터 배열 해시 표 는 체인 주소 법 으로 충돌 을 처리 합 니 다.
create 를 실현 하 세 요hash 함수, 지정 한 크기 의 해시 표를 만 듭 니 다.
#include
HashTable* create_hash(int size)
{
HashTable* H = (HashTable*)malloc(sizeof(HashTable));
H->bucket = (HashEntry**)malloc(sizeof(HashEntry**) * size);
if (!H->bucket) {
free(H);
return NULL;
}
memset(H, 0, sizeof(HashTable));
H->size = size;
return H;
}