Data structure — hash table

Time:2021-10-16

A go language implementation of hash table

package main

import (
    "fmt"
    "os"
)

//Information of data in linked list
type Emp struct {
    Id int
    Name string
    Next *Emp
}

func (e *Emp) ShowSelf() {
    FMT. Printf ("linked list% d found this node% d \ n", e.id% 7, e.id ")
}

//Define a linked list that does not have a leading node
type EmpLink struct {
    Head *Emp
}

//How to add emplink
func (l *EmpLink) Insert (emp *Emp) {
    //Define two auxiliary pointers
    cur := l.Head
    var pre *Emp = nil

    //Determine whether it is an empty linked list
    If cur = = nil {// insert linked list header
        l.Head = emp
        //Cur = EMP // this will cause the node to not be added. I don't know why
        return
    }

    //If it is not an empty linked list, find the corresponding position for EMP and insert it
    for {
        if cur !=  Nil {// insert in the middle of the linked list
            if cur.Id >= emp.Id {
                pre.Next = emp
                emp.Next = cur
                break
            }
            pre = cur
            cur = cur.Next
        }Else {// insert at the end of the linked list
            pre.Next = emp
            emp.Next = cur
            break
        }
    }
}

func (l *EmpLink)FindId(id int) *Emp{
    cur := l.Head
    for {
        if cur != nil && cur.Id == id {
            return cur
        }else if cur == nil {
            break
        }
        cur = cur.Next
    }
    return nil
}

//Define a method to display linked list elements
func (l *EmpLink) ShowLink(no int) {
    if l.Head == nil {
        FMT. Printf ("linked list% d is empty \ n", no)
        return
    }

    //Traverse the current linked list and display data
    cur := l.Head
    for {
        if cur != nil {
            FMT. Printf ("linked list% d node id =% d name =% s - >", no, cur. ID, cur. Name)
            cur = cur.Next
        } else {
            break
        }
    }
    fmt.Println()
}

//Define a hash table with 7 linked lists
type HashTable struct {
    LinkArr [7]EmpLink
}

//Adding method of hashtable
func (h *HashTable) Insert (emp *Emp) {
    //Use the hash function to determine which linked list the node is added to
    LinkNo := h.HashFun(emp.Id)
    //Use the corresponding linked list to add
    h.LinkArr[LinkNo].Insert(emp)
}

func (h *HashTable) Find(no int) *Emp {
    lindNo := h.HashFun(no)
    return  h.LinkArr[lindNo].FindId(no)
}
//Show all hashtable nodes
func (h *HashTable) ShowAll() {
    for i := 0; i < len(h.LinkArr); i++ {
        h.LinkArr[i].ShowLink(i)
    }
}

func (h *HashTable) HashFun (id int) int {
    Return ID% 7 // returns the index of the linked list
}

func main(){
    var hashtable HashTable
    key := ""
    id := 0
    name := ""
    for {
        FMT. Println ("system menu ============================================")
        Fmt.println ("input add node")
        Fmt.println ("show display node")
        Fmt.println ("find node")
        Fmt.println ("exit exit system")
        Fmt.println ("please enter your choice")
        Fmt.println ("please enter your choice")
        fmt.Scanln(&key)
        switch key {
        case "input":
            Fmt.println ("enter node ID")
            fmt.Scanln(&id)
            Fmt.println ("input node name")
            fmt.Scanln(&name)
            emp := &Emp{
                Id:   id,
                Name: name,
            }
            hashtable.Insert(emp)
        case "show":
            hashtable.ShowAll()
        case "find":
            Fmt.println ("please enter the ID number you want to find")
            fmt.Scanln(&id)
            emp := hashtable.Find(id)
            if emp == nil {
                FMT. Println ("node with id =% d does not exist \ n", ID)
            }else {
                emp.ShowSelf()
            }
        case "exit":
            os.Exit(0)
        default:
            Fmt.println ("input error")
        }
    }
}