-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashTable.go
124 lines (88 loc) · 1.88 KB
/
hashTable.go
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
package main
import "fmt"
type set struct {
key int
data int
}
const capacity = 10
var size = 0
var array = new([10]set)
func init_array() {
for i := 0; i < capacity; i++ {
array[i].key = 0
array[i].data = 0
}
}
func hashFunction(key int) int {
return (key % capacity)
}
func insert(key, value int) {
index := hashFunction(key)
if array[index].key == 0 {
array[index].key = key
array[index].data = value
size++
fmt.Printf("Key (%d) has been inserted\n", key)
} else if array[index].key == key {
array[index].data = value
} else {
fmt.Println("Collision occured")
}
}
func removeElement(key int) {
index := hashFunction(key)
if array[index].data == 0 {
fmt.Println("This key does not exits")
} else {
array[index].key = 0
array[index].data = 0
size--
fmt.Printf("Key (%d) has been removed", key)
}
}
func sizeHashTable() int {
return size
}
func Display() {
for i := 0; i < capacity; i++ {
if array[i].data == 0 {
fmt.Printf("\narray[%d]: /", i)
} else {
fmt.Printf("\nKey: %d array[%d]: %d\t", array[i].key, i, array[i].data)
}
}
}
func main() {
var key, data, choice int
init_array()
c := 1
for c != 0 {
fmt.Println("1. Insert item in the Hash Table")
fmt.Println("2. Remove item from in the Hash Table")
fmt.Println("3. Check the size of Hash Table")
fmt.Println("4. Display Hash Table")
fmt.Printf("Please Enter you choice: ")
fmt.Scan(&choice)
switch choice {
case 1:
fmt.Printf("Enter key: ")
fmt.Scan(&key)
fmt.Printf("Enter key: ")
fmt.Scan(&data)
insert(key, data)
case 2:
fmt.Printf("Enter the key Remove: ")
fmt.Scan(&key)
removeElement(key)
case 3:
n := sizeHashTable()
fmt.Println("Size Hash Table: ", n)
case 4:
Display()
default:
fmt.Println("Invalid input")
}
fmt.Printf("\nDo you want to continue (press 1 for yes):")
fmt.Scanf("%d", &c)
}
}