-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable_Chaining.cpp
More file actions
78 lines (61 loc) · 1.36 KB
/
Copy pathHashTable_Chaining.cpp
File metadata and controls
78 lines (61 loc) · 1.36 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct data{
char key[51];
struct data *next;
struct data *prev;
char nim[11];
int tinggi;
float berat;
}*head[26], *tail[26], *curr;
int hashFirstChar(char insertVall[51]){
int hashKey = insertVall[0] - 97;
curr = (struct data*) malloc(sizeof(struct data));
curr->next = NULL;
strcpy(curr->key, insertVall);
if(head[hashKey] == NULL){
head[hashKey] = tail[hashKey] = curr;
}else{
tail[hashKey]->next = curr;
tail[hashKey] = curr;
}
tail[hashKey]->next = NULL;
}
void popAll(){
int i;
for(i = 0; i < 26; i++){
curr = head[i];
while(curr != NULL){
head[i] = head[i]->next;
free(curr);
curr = head[i];
}
}
}
void print(struct data h[26]){
int i;
printf("Index | Key\n");
for(int i; i < 26; i++){
curr = head[i];
printf("%5d | ", i);
while(curr != NULL){
printf("%-10s -> ", curr->key);
curr = curr->next;
}
printf("\n");
}
}
int main(){
char insertVal[51];
struct data h[26] = {};
do{
print(h);
printf("Masukan Key : ");
scanf("%s", insertVal);
getchar();
hashFirstChar(insertVal);
}while(strcmp(insertVal, "Exit") != 0);
popAll();
return 0;
}