Golang实现一个简单的单链表
package listtype Node struct {
Data interface{}
Next *Node
}type List struct {
size uint32
head *Node
tail *Node
}// 构造方法,将所有变量置空
func (list *List) init() {
(*list).size = 0
(*list).head = nil
(*list).tail = nil
}func (list *List) Append(node *Node) bool {
// 若 node 为空,直接返回
if nil == node {
return false
} // 若 List 没有节点--头插
// 若 List 存在节点--尾插
if 0 == (*list).size {
(*list).head = node
} else {
oldTail := (*list).tail
(*oldTail).Next = node
} (*list).tail = node
(*list).size++ return true
}func (list *List) Insert(index uint32, node *Node) bool {
if nil == node || index >= (*list).size || 0 == (*list).size {
return false
} // 如果 index 为0--头插
if 0 == index {
(*node).Next = (*list).head
(*list).head = node
} else {
preNode := (*list).head
for i := 1;
uint32(i) < index;
i++ {
preNode = (*preNode).Next
}(*node).Next = (*preNode).Next
(*preNode).Next = node
} (*list).size++
return true
}func (list *List) Remove(index uint32) bool {
if index >= (*list).size {
return false
} if 0 == index { // 头删
(*list).head = (*list).head.Next
if 1 == (*list).size {
(*list).tail = nil
}
} else {
preNode := (*list).head
for i := 1;
uint32(i) < index;
i++ {
preNode = (*preNode).Next
}
(*preNode).Next = (*preNode).Next.Nextif index == (*list).size- 2 {
(*list).tail = preNode
}
} (*list).size-- return true
}func (list *List) IndexOf(index uint32) *Node {
if index >= (*list).size {
return nil
} node := (*list).head
for i := 0;
uint32(i) < index;
i++ {
node = (*node).Next
} return node
}func (list *List) Size() uint32 {
return (*list).size
}func (list *List) Head() *Node {
return (*list).head
}func (list *List) Tail() *Node {
return (*list).tail
}
推荐阅读
- Go|Docker后端部署详解(Go+Nginx)
- GO|GO,GO,GO!
- Go成长之路|go中判断空字符串、nil和len(t)的用法
- go编译tools
- go grpc安装与使用
- goroutine 调度原理
- Go|Go进阶之路——复杂类型
- Go进阶之路——变量
- Go进阶之路——流程控制语句