Skip to content

Commit

Permalink
Go idiom
Browse files Browse the repository at this point in the history
  • Loading branch information
youngzhu committed Dec 15, 2023
1 parent ab44242 commit 6173985
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
20 changes: 10 additions & 10 deletions searching/sequential_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ package searching
// using equals() to compare the search key with the key in each node in th list.
// If we find the match, we return the associated value; if not, we return nil.
// To implement put(), we also scan through the list, using equals() to compare
// the client key with the key in each node in the list. If we find the match,
// the client key with the key in each node in the list. If we find the match,
// we update the value associated with that key to be the value given; if not,
// we create a new node with the given key and value, and insert it at the
// we create a new node with the given key and value, and insert it at the
// beginning of the list. This method is known as sequential search.

type SequentialSearchST struct {
n int // number of key-value pairs
n int // number of key-value pairs
first *node // the linked list of key-value pairs
}

// a helper linked list data type
type node struct {
key STKey
key STKey
value STValue
next *node
next *node
}

func NewSequentialSearchST() *SequentialSearchST {
return &SequentialSearchST{}
}

// Returns the value associated with the given key
// Get returns the value associated with the given key
// in this symbol table
func (st *SequentialSearchST) Get(key STKey) STValue {
if key == nil {
Expand All @@ -41,7 +41,7 @@ func (st *SequentialSearchST) Get(key STKey) STValue {
return nil
}

// Inserts the specified key-value pair into the symbol table,
// Put inserts the specified key-value pair into the symbol table,
// overwriting the old value with the new value if the symbol
// table already contains the specified key.
// Deletes the specified key (and its associated value) from the
Expand All @@ -66,7 +66,7 @@ func (st *SequentialSearchST) Put(key STKey, value STValue) {
st.n++
}

// Remove the specified key and its associated value from the symbol
// Delete remove the specified key and its associated value from the symbol
// table (if the key is in the symbol table)
func (st *SequentialSearchST) Delete(key STKey) {
if key == nil {
Expand All @@ -89,7 +89,7 @@ func (st *SequentialSearchST) deleteNode(x *node, key STKey) *node {
return x
}

// Return all keys in the symbol table
// Keys return all keys in the symbol table
func (st *SequentialSearchST) Keys() []STKey {
var keys []STKey
for x := st.first; x != nil; x = x.next {
Expand All @@ -100,4 +100,4 @@ func (st *SequentialSearchST) Keys() []STKey {

func (st *SequentialSearchST) Contains(key STKey) bool {
return st.Get(key) != nil
}
}
15 changes: 7 additions & 8 deletions searching/symbol_table.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package searching

// Symbol table (ST)
// The primary prupose of a symbol table is to associate a value with a key.
// The client can insert key-value pairs into the symbol table with the expection
// SymbolTable (ST)
// The primary purpose of a symbol table is to associate a value with a key.
// The client can insert key-value pairs into the symbol table with the expectation
// of later being able to search for the value associated with a given key.

type SymbolTable interface {
Put(key STKey, value STValue)
Get(key STKey) STValue
Expand All @@ -13,8 +12,8 @@ type SymbolTable interface {
Keys() []STKey
}

// The key in ST
type STKey interface {}
// STKey the key in ST
type STKey interface{}

// The value in ST
type STValue interface{}
// STValue the value in ST
type STValue interface{}

0 comments on commit 6173985

Please sign in to comment.