Hash table of a Go language implementation

package main

import (
	"fmt"
	"os"
)

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

func (e *Emp) ShowSelf(a) {
	fmt.Printf("Linked list %d found the node %d\n", e.Id % 7, e.Id)
}

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

// Add method to EmpLink
func (l *EmpLink) Insert (emp *Emp) {
	// Define two auxiliary Pointers
	cur := l.Head
	var pre *Emp = nil

	// Check whether the list is empty
	if cur == nil { // List header inserts
		l.Head = emp
		//cur = emp
		return
	}

	// If the list is not empty, find the corresponding position for emP and insert it
	for {
		ifcur ! =nil { // Insert in the middle of the list
			if cur.Id >= emp.Id {
				pre.Next = emp
				emp.Next = cur
				break
			}
			pre = cur
			cur = cur.Next
		} else { // Insert the end of the list
			pre.Next = emp
			emp.Next = cur
			break}}}func (l *EmpLink)FindId(id int) *Emp{
	cur := l.Head
	for {
		ifcur ! =nil && cur.Id == id {
			return cur
		}else if cur == nil {
			break
		}
		cur = cur.Next
	}
	return nil
}

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

	// Iterate over the current list and display the data
	cur := l.Head
	for {
		ifcur ! =nil {
			fmt.Printf("List %d node Id= %d name =%s ->", no, cur.Id, cur.Name)
			cur = cur.Next
		} else {
			break
		}
	}
	fmt.Println()
}

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

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

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

func (h *HashTable) HashFun (id int) int {
	return id % 7 // return the list subscript
}

func main(a){
	var hashtable HashTable
	key := ""
	id := 0
	name := ""
	for {
		fmt.Println("==================== System menu =====================")
		fmt.Println("Input add node")
		fmt.Println("Show show node")
		fmt.Println("Find")
		fmt.Println(Exit the 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("Enter 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 are looking for.")
			fmt.Scanln(&id)
			emp := hashtable.Find(id)
			if emp == nil {
				fmt.Println("Id =%d does not exist \n", id)
			}else {
				emp.ShowSelf()
			}
		case "exit":
			os.Exit(0)
		default:
			fmt.Println("Input error")}}}Copy the code