-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
51 lines (44 loc) · 959 Bytes
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Created by ge on 2023/12/31.
//
#include <stdio.h>
#include <stdlib.h>
#define NUM 5
typedef struct HashList {
int num;
char *data;
} HashList;
HashList *initList() {
HashList *list = (HashList *) malloc(sizeof(HashList));
list->num = 0;
list->data = (char *) malloc(sizeof(char) * NUM);
for (int i = 0; i < NUM; ++i) {
list->data[i] = 0;
}
}
int hash(int data) {
return data % NUM;
}
void put(HashList *list, char data){
int index = hash(data);
if (list->data[index] != 0){
int count = 1;
while (list->data[index] != 0) {
index = hash(hash(data) + count);
count++;
}
}
list->data[index] = data;
}
int main() {
HashList *list = initList();
put(list,'A');
put(list,'B');
put(list,'C');
put(list,'D');
put(list,'E');
put(list,'F');
printf("%c\n",list->data[0]);
printf("%c\n",list->data[1]);
return 0;
}