-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashMap.c
More file actions
136 lines (114 loc) · 3.2 KB
/
hashMap.c
File metadata and controls
136 lines (114 loc) · 3.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#pragma warning (disable:4819)
//
// Created by JakoError on 2020/12/26.
//
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "hashMap.h"
int hash(char key[]) {
int hash = 0;
if (key != NULL)//null 返回0
for (int i = 0; i < strlen(key); ++i)
hash = 31 * hash + key[i];
return abs(hash);//非负
}
HashMap createHashMap(KEY_TYPE keys[], VALUE_TYPE values[], int size) {
if (keys == NULL && values == NULL && size != 0) {
printf("\nFAILED TO CREAT MAP:SIZE is not equal to 0 when Keys and Value are null\n");
return NULL;
}
//----------------------------------------------------------预处理
HashMap hm = (HashMap) malloc(sizeof(struct hashmap));
if (hm == NULL) {
printf("error in malloc HashMap\n");
exit(0);
}
clear(hm);
if (!size)
return hm;
//-----------------------------------------------------------插入值
//插入所有值
if (!putAllData(hm, keys, values, size))
return NULL;
return hm;
}
HashNode newNode() {
HashNode pNode = malloc(sizeof(struct node));
if (pNode == NULL) {
printf("error in malloc HashNode\n");
exit(0);
}
memset(pNode, 0, sizeof(struct node));
return pNode;
}
bool putData(HashMap hm, KEY_TYPE key, VALUE_TYPE value) {
if (hm == NULL || key == NULL)
return false;
int index = hash(key) % DEFAULT_MAP_LENGTH;
HashNode p = &(hm->map[index]);
while (!(p->data.key == NULL || strcmp(p->data.key, key) == 0 || p->next == NULL)) {
p = p->next;
}
//不允许复写
if(p->data.key!=NULL) return false;
p->data.key = malloc(sizeof(key));
strcpy(p->data.key, key);
p->data.value = value;
//prepare for next(mainly to avoid jump to NULL)
p->next = newNode();
hm->size++;
return true;
}
bool putAllData(HashMap hm, KEY_TYPE key[], VALUE_TYPE value[], int size) {
int ok = 0;
for (int i = 0; i < size; ++i) {
if (putData(hm, key[i], value[i]))
ok++;
}
printf("\n填装%d个数据:成功%d个\n", size, ok);
return ok == size;
}
//不想写了,我这用不到
bool delete(HashMap hm, KEY_TYPE key);
VALUE_TYPE *getValue(HashMap hm, KEY_TYPE key) {
int index = hash(key) % DEFAULT_MAP_LENGTH;
HashNode p = &hm->map[index];
if (p->next == NULL)
return NULL;
if (p->next->next != NULL)
while (strcmp(p->data.key, key) != 0) {
p = p->next;
if (p == NULL)
return NULL;
}
return &p->data.value;
}
DataType *getAllData(HashMap hm) {
static DataType list[100];
DataType *l = list;
for (int i = 0; i < DEFAULT_MAP_LENGTH; ++i) {
HashNode p = &hm->map[i];
if (p->next == NULL)
continue;
while (p->next != NULL) *(l++) = p->data;
}
return list;
}
void clear(HashMap hm) {
memset(hm, 0, sizeof(struct hashmap));
hm->size = 0;
}
void freeHashMap(HashMap hm) {
if (hm == NULL) return;
for (int i = 0; i < DEFAULT_MAP_LENGTH; ++i) {
HashNode p = hm->map[i].next;
while (p != NULL) {
HashNode temp = p;
p = p->next;
free(temp);
}
}
free(hm);
}