-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds22_a.cpp
82 lines (72 loc) · 1.89 KB
/
ds22_a.cpp
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
#include <stdio.h>
#include <stdlib.h>
int count = 0;
void divisionkey(int key, int bucket, int slot, int** tableset) {
int identifier = key % bucket;
while (count < bucket * slot) {
for (int slotnumber = 0; slotnumber < slot; slotnumber++) {
if (tableset[identifier][slotnumber] == -1) {
tableset[identifier][slotnumber] = key;
count++;
return;
}
}
if (identifier == bucket - 1)
identifier = 0;
else
identifier++;
}
}
int searchkey(int value, int bucket, int slot, int** tableset) {
int identifier = value % bucket;
int searchcount = 0;
while (searchcount < bucket * slot) {
for (int slotnumber = 0; slotnumber < slot; slotnumber++) {
if (tableset[identifier][slotnumber] == value)
return identifier;
else
searchcount++;
}
if (identifier == bucket - 1)
identifier = 0;
else
identifier++;
}
return -1;
}
int main() {
FILE* hashnumber;
fopen_s(&hashnumber, "in.txt", "r");
int bucketsize, slotsize;
fscanf_s(hashnumber, "%d", &bucketsize);
fscanf_s(hashnumber, "%d", &slotsize);
int** hashtable = (int**)malloc(sizeof(*hashtable) * bucketsize);
for (int i = 0; i < bucketsize; i++) {
hashtable[i] = (int*)malloc(sizeof(**hashtable) * slotsize);
for (int j = 0; j < slotsize; j++)
hashtable[i][j] = -1;
}
int keynumber;
while (!feof(hashnumber)) {
fscanf_s(hashnumber, "%d", &keynumber);
divisionkey(keynumber, bucketsize, slotsize, hashtable);
}
fclose(hashnumber);
printf("Hash Table\n");
for (int hash = 0; hash < bucketsize; hash++) {
printf("%d: ", hash);
for (int slot = 0; slot < slotsize; slot++) {
if (hashtable[hash][slot] != -1)
printf("%d ", hashtable[hash][slot]);
}
printf("\n");
}
printf("key °Ë»ö : ");
scanf_s("%d", &keynumber);
while (keynumber != -999) {
printf("%d\n", searchkey(keynumber, bucketsize, slotsize, hashtable));
printf("key °Ë»ö : ");
scanf_s("%d", &keynumber);
}
return 0;
}