-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearHash.c
75 lines (70 loc) · 1.61 KB
/
linearHash.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
typedef struct dataItem ITEM;
struct dataItem {
int data;
int key;
};
ITEM* hashArr[SIZE];
ITEM* dummyItem;
ITEM* item;
int hashCode(int key) {
return (key % SIZE);
}
ITEM* search(int key) {
int hashIndex = hashCode(key);
while (hashArr[hashIndex] != NULL) {
if (hashArr[hashIndex]->key == key) {
return hashArr[hashIndex];
}
hashIndex++;
hashIndex %= SIZE;
}
return NULL;
}
void insert(int key, int data)
{
ITEM *item = (ITEM*) malloc(sizeof(ITEM));
item->data = data;
item->key = key;
int hashIndex = hashCode(key);
while(hashArr[hashIndex] != NULL && hashArr[hashIndex]->key != -1) {
++hashIndex;
hashIndex %= SIZE;
}
hashArr[hashIndex] = item;
}
ITEM *delete(ITEM *item){
int hashIndex = hashCode(item->key);
while(hashArr[hashIndex] != NULL){
if(hashArr[hashIndex]->key == item->key) {
ITEM *temp = hashArr[hashIndex];
hashArr[hashIndex] = dummyItem;
return temp;
}
++hashIndex;
hashIndex%=SIZE;
}
return NULL;
}
void display(){
for(int i = 0; i < SIZE; i++) {
if(hashArr[i] != NULL){
printf("(%d,%d) ", hashArr[i]->key, hashArr[i]->data);
}
else printf("~~~");
}
printf("\n");
}
int main() {
dummyItem = (ITEM*)malloc(sizeof(ITEM));
dummyItem->key = -1;
dummyItem->data = -1;
insert(1,20);
insert(21, 3);
insert(40, 21);
insert(66, 23);
display();
return 0;
}