golang hash hash table data structures (the Hash)

hash.go

the hash Package 

Import ( 
    " FMT " 
) 

type Emp struct { 
    ID    int 
    the Name String 
    the Next * Emp 
} 

// first node to store employee 
type EmpLink struct { 
    Head * Emp 
} 

// define the HashTable 
type the HashTable struct { 
    LinkArr [ . 7 ] EmpLink 
} 

// method of adding employees 
FUNC (Empl * EmpLink) InsertEmp (EMP * Emp) { 
    CUR: = empl.Head
     var* = Emp pre nil
     IF CUR == nil { 
        empl.Head = EMP
         return 
    } 
    // if it is not an empty list, find the corresponding position and insert 
    for {
         IF CUR! = nil {
             IF cur.ID> = emp.ID {
                 BREAK 
            } 
            pre = CUR 
            CUR = cur.Next 
        } the else {
             BREAK 
        } 
    } 
    pre.Next = EMP 
    emp.Next = CUR
} 
FUNC (the hash * the HashTable) the Insert (* EMP Emp) {
     // hash function, which is determined to add the employee list 
    linkNum: = hash.HashFunc (emp.ID) 
    hash.LinkArr [linkNum] .InsertEmp (EMP) 
} 

FUNC (Empl * EmpLink) findById (ID int ) * Emp { 
    CUR: = empl.Head
     for {
         IF CUR = nil && cur.ID ==! ID {
             return CUR 
        } the else  IF CUR == nil {
             BREAK 
        } 
        CUR = cur.Next 
    }
    return nil 
} 

FUNC (the hash * the HashTable) the Find (ID int ) * Emp {
     // hash function to determine which list in 
    linkNum: = hash.HashFunc (ID)
     return hash.LinkArr [linkNum] .FindByID (ID) 
} 

/ / hashing method 
FUNC (the hash the HashTable *) HashFunc (ID int ) int {
     return ID% . 7 
} 
FUNC (Empl * EmpLink) ShowLink (NUM int ) {
     IF empl.Head == nil { 
        fmt.Printf ( " current% d the list is empty \ the n- " , NUM)
         return
    } 
    // otherwise traversing the display data 
    CUR: = empl.Head
     for {
         IF CUR =! Nil { 
            fmt.Printf ( " list:% d, employee id:% d, employee name:% S -> " , NUM, CUR .ID, cur.Name) 
            CUR = cur.Next 
        } the else {
             BREAK 
        } 
    } 
    fmt.Println ( ``) 
} 

FUNC (the hash * the HashTable) the Show () {
     for I: = 0 ; I <len (hash.LinkArr) ; I ++ { 
        hash.LinkArr [I] .ShowLink (I) 
    } 
}

FUNC (EMP* Emp) ShowMe () { 
    fmt.Printf ( " list to find the employee% d% d \ n- " , emp.ID% . 7 , emp.ID) 
}

main.go

main Package 

Import ( 
    " FMT " 
    " go_code / data_structure / the hash " 
    " OS " 
) 

FUNC main () { 

    Key: = "" 
    ID: = 0 
    name: = "" 
    var hashTable hash.HashTable
     for { 
        fmt.Println ( " = ========= employees menu ========== " ) 
        fmt.Println ( " INSERT add employees represented " ) 
        fmt.Println ( " show displayed represents employees " ) 
        fmt.Println("Employees find indicates that the query " ) 
        fmt.Println ( " Exit to Quit employees " ) 
        fmt.Println ( " Please enter your selection: " ) 
        fmt.Scanln ( & Key)
         Switch Key {
         Case  " INSERT " : 
            fmt.Println ( " Please enter the employee the above mentioned id: " ) 
            fmt.Scanln ( & the above mentioned id) 
            fmt.Println ( " Please enter the employee name: " ) 
            fmt.Scanln ( & name) 
            emp: = &hash.Emp{
                ID: id, 
                the Name: name, 
            } 
            hashTable.Insert (emp) 
        Case  " Show " : 
            hashTable.Show () 
        Case  " the Find " : 
            fmt.Println ( " Please enter the id you want to find: " ) 
            fmt.Scanln ( & ID) 
            EMP: = hashTable.Find (ID)
             IF EMP == nil { 
                fmt.Printf ( " ID =% n D staff does not exist \ " , ID) 
            } the else {
                // display employee information 
                emp.ShowMe () 

            } 
        Case  " Exit " : 
            os.Exit ( 0 ) 
        } 
    } 
}

operation result:

f: \ goproject \ src \ go_code \ data_structure> Go RUN main.go
========== employees menu ==========
INSERT represent add employees
show display represents employees
find employees represent the query
exit means exit of employees
Please enter your choice:
INSERT
enter employees the above mentioned id:
1
Please enter the employee name:
bob
========== employees menu ==========
INSERT represent added staff
show display represents employees
find indicates that the query staff
exit to quit employees
Please enter your choice:
INSERT
enter employees the above mentioned id:
8
Please enter the employee name:
Mike
========== ======== employees menu ==
INSERT represent add employees
show display represents employees
find indicates that the query staff
exit to quit employees
Please enter your choice:
INSERT
enter employees the above mentioned id:
15
Please enter the employee name:
tom
========== employees menu = =========
insert represents added staff
show display represents employees
find indicates that the query staff
exit to Quit employees
Please enter your choice:
insert
please enter the employee the above mentioned id:
57
Please enter the employee name:
POP
========== === employees menu =======
INSERT represent add employees
show display represents employees
find indicates that the query staff
exit to quit employees
Please enter your choice:
show
the current list is empty 0
list: 1 employee id: 1, name employees: bob-- > list: 1 employee id: 8, employee name: mike -> list: 1 employee id: 15, employee name: tom -> list: 1 employee id: 57, employee name: pop ->
current 2 list is empty
current list is empty 3
current 4 list is empty
the current 5 list is empty
current list is empty 6
========== employees menu ==========
INSERT add staff representation
show display represents employees
find indicates that the query staff
exit to quit employees
Please enter your choice:
INSERT
enter employees the above mentioned id:
36
Please enter the employee name:
BIB
========== employees menu ==========
INSERT represent add employees
show display represents employees
find indicates that the query staff
exit to Quit employees
Please enter your choice:
show
the current list is empty 0
list: 1 employee id: 1, staff name: bob -> list: 1 employee id: 8, employee name: mike -> list: 1 employee id: 15, employee name: tom -> list: 1 employee id: 36, employee name: bib -> list: 1 employee id: 57, employee name: pop ->
current 2 list is empty
current list is empty 3
current 4 list is empty
current list is empty 5
the current list is empty 6
========== employees menu ==========
INSERT represent add employees
show display represents employees
find indicates that the query staff
exit to quit employees
Please enter your choice:
INSERT
Please enter the employee the above mentioned id:
12
Please enter the employee name:
viv
========== employees menu ==========
INSERT represent add employees
show display represents employees
find indicates that the query staff
exit for quit employees
Please enter your choice:
Show
The current list is empty 0
list: 1 employee id: 1, staff name: bob -> list: 1 employee id: 8, employee name: mike -> list: 1 employee id: 15, the names of employees: tom -> list: 1 employee id: 36, employee name: bib -> list: 1 employee id: 57, employee name: pop ->
The current list is empty 2
current 3 list is empty
current list is empty 4
list: 5, employee id: 12, employee name: viv ->
The current list is empty 6
========== employees menu ==========
INSERT represent add employees
show indicate display employees
find indicates that the query staff
exit to quit employees
Please enter your choice:
find
your input id you are looking for:
12
list 5 found that 12 employees
========== ======= employees menu ===
INSERT represent add employees
show display represents employees
find indicates that the query staff
exit to quit employees
Please enter your choice:
find
your input id you are looking for:
7
id = 7 employees does not exist
======== == employees menu ==========
INSERT represent add employees
show display represents employees
find employees represent the query
exit means exit of employees
Please enter your choice:
exit

f:\goproject\src\go_code\data_structure>

Guess you like

Origin www.cnblogs.com/xiximayou/p/12029622.html