-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsequential_search_st.go
74 lines (62 loc) · 1.44 KB
/
sequential_search_st.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
package algo
type node struct {
key, val interface{}
next *node
}
// SequentialSearchST is symbol table
type SequentialSearchST struct {
first *node
n int
}
// NewSequentialSearchST ...
func NewSequentialSearchST() *SequentialSearchST {
return &SequentialSearchST{}
}
// Put add new key value pair into the st
func (st *SequentialSearchST) Put(key, val interface{}) {
for x := st.first; x != nil; x = x.next {
if x.key == key {
x.val = val
return
}
}
st.first = &node{key, val, st.first}
st.n++
}
// Get add new key value pair into the st
func (st *SequentialSearchST) Get(key interface{}) interface{} {
for x := st.first; x != nil; x = x.next {
if x.key == key {
return x.val
}
}
return nil
}
// GetInt return the val as int( have to do this since GO doesn't have generics)
func (st *SequentialSearchST) GetInt(key interface{}) int {
return st.Get(key).(int)
}
// Delete ...
func (st *SequentialSearchST) Delete(key interface{}) {
st.Put(key, nil)
}
// Contains ...
func (st *SequentialSearchST) Contains(key interface{}) bool {
return st.Get(key) != nil
}
// Size ...
func (st *SequentialSearchST) Size() int {
return st.n
}
// IsEmpty add new key value pair into the st
func (st SequentialSearchST) IsEmpty() bool {
return st.Size() == 0
}
// Keys ...
func (st *SequentialSearchST) Keys() []interface{} {
var keys []interface{}
for x := st.first; x != nil; x = x.next {
keys = append(keys, x.key)
}
return keys
}