Go 实现 LinkTable 库

aaa 2022-04-02 19:58:08

一、单链表实现

  1. 链表包含头节点和长度
type Node struct {
    data interface{}
    next *Node
}

type List struct {
    length int
    head   *Node
}
  1. 初始化和判空
func (list *List) IsEmpty() bool {
    if list.length == 0 {
        return true
    } else {
        return false
    }
}

func InitList() *List {
    node := new(Node)
    L := new(List)
    L.head = node
    return L
}
  1. 插入

头插、尾插、指定位置

func (list *List) PushFront(v interface{}) {
    node := &Node{data: v}
    if list.IsEmpty() {
        list.head = node
    } else {
        node.next = list.head
        list.head = node
    }
    list.length++
}

func (list *List) PushBack(v interface{}) {
    node := &Node{data: v}
    if list.IsEmpty() {
        list.head = node
    } else {
        cur := list.head
        for cur.next != nil {
            cur = cur.next
        }
        cur.next = node
    }
    list.length++
}

func (list *List) InsertElem(index int, v interface{}) {
    if index <= 0 || index > list.length {
        fmt.Println("err")
    } else {
        pre := list.head
        node := &Node{data: v}
        if index == 1 {
            node.next = pre
            list.head = node
        } else {
            for count := 1; count < index-1; count++ {
                pre = pre.next
            }
            node.next = pre.next
            pre.next = node
        }
        list.length--
    }
}
  1. 删除

元素值、元素下标

func (list *List) DeleteElem(index int) {
    if index <= 0 || index > list.length {
        fmt.Println("wrong index")
    } else {
        pre := list.head
        if index == 1 {
            list.head = pre.next
        } else {
            pre := list.head
            for count := 1; count < index-1; count++ {
                pre = pre.next
            }
            pre.next = pre.next.next
        }
        list.length--
    }
}

func (list *List) Remove(v interface{}) {
    pre := list.head
    if pre.data == v {
        list.head = pre.next
    } else {
        for pre.next != nil {
            if pre.next.data == v {
                pre.next = pre.next.next
                return
            } else {
                pre = pre.next
            }
        }
        fmt.Println("fail")
        return
    }
}
  1. 获取元素

下标得值、值得下标

func (list *List) GetData(index int) int {
    if index <= 0 || index > list.length {
        return -1
    } else {
        pre := list.head
        for j := 0; j < index-1; j++ {
            if pre != nil {
                pre = pre.next
            }
        }
        return pre.data.(int)
    }
}

func (list *List) GetIdx(v interface{}) int {
    cnt := 0
    pre := list.head
    for pre != nil {
        cnt++
        if pre.data == v {
            return cnt
        }
        pre = pre.next
    }
    return -1
}

二、测试

img

img

可以查看测试覆盖多少源码范围

Github Link

作者:612

...全文
140 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

571

社区成员

发帖
与我相关
我的任务
社区描述
软件工程教学新范式,强化专项技能训练+基于项目的学习PBL。Git仓库:https://gitee.com/mengning997/se
软件工程 高校
社区管理员
  • 码农孟宁
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧