Go实战--实现一个单向链表(The way to go)
生命不止,继续 go go go !!! 今天跟大家分享一下数据介绍相关的实现吧,那就已链表为例吧。 golang中为我们提供了一个list,我们先了解一下container/list。 container/list功能: 看清楚了吧,是双向链表。 一些方法: func (e *Element) Prev() *Element func New() *List func (l *List) Back() *Element func (l *List) Front() *Element func (l *List) Init() *List func (l *List) InsertAfter(v interface{},mark *Element) *Element func (l *List) InsertBefore(v interface{},mark *Element) *Element func (l *List) Len() int func (l *List) MoveAfter(e,mark *Element) func (l *List) MoveBefore(e,mark *Element) func (l *List) MoveToBack(e *Element) func (l *List) MoveToFront(e *Element) func (l *List) PushBack(v interface{}) *Element func (l *List) PushBackList(other *List) func (l *List) PushFront(v interface{}) *Element func (l *List) PushFrontList(other *List) func (l *List) Remove(e *Element) interface{} 简单的应用: package main
import (
"container/list"
"fmt"
)
func main() {
// 创建一个链表
alist := list.New()
fmt.Println("Size before : ",alist.Len())
//向链表中添加元素
alist.PushBack("a")
alist.PushBack("b")
alist.PushBack("c")
fmt.Println("Size after insert(push): ",alist.Len())
// 遍历
for e := alist.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value.(string))
}
//移除元素
alist.Remove(alist.Front())
alist.Remove(alist.Front())
alist.Remove(alist.Front())
fmt.Println("Size after remove(pop) : ",alist.Len())
}
输出: 实现一个单向链表go为我们提供的list是一个双向链表,那么我们自己实现一个单向链表吧。 声明节点 package singly_linked_list
type Node struct {
Value interface{}
next *Node
}
func (n *Node) Next() *Node {
return n.next
}
type SinglyLinkedList struct {
front *Node
length int
}
初始化 func (s *SinglyLinkedList) Init() *SinglyLinkedList {
s.length = 0
return s
}
New一个链表 func New() *SinglyLinkedList {
return new(SinglyLinkedList).Init()
}
返回头结点 func (s *SinglyLinkedList) Front() *Node {
return s.front
}
返回尾结点 func (s *SinglyLinkedList) Back() *Node {
currentNode := s.front
for currentNode != nil && currentNode.next != nil {
currentNode = currentNode.next
}
return currentNode
}
添加尾结点 func (s *SinglyLinkedList) Append(n *Node) {
if s.front == nil {
s.front = n
} else {
currentNode := s.front
for currentNode.next != nil {
currentNode = currentNode.next
}
currentNode.next = n
}
s.length++
}
添加头结点 func (s *SinglyLinkedList) Prepend(n *Node) {
if s.front == nil {
s.front = n
} else {
n.next = s.front
s.front = n
}
s.length++
}
在指定结点前添加结点 func (s *SinglyLinkedList) InsertBefore(insert *Node,before *Node) {
if s.front == before {
insert.next = before
s.front = insert
s.length++
} else {
currentNode := s.front
for currentNode != nil && currentNode.next != nil && currentNode.next != before {
currentNode = currentNode.next
}
if currentNode.next == before {
insert.next = before
currentNode.next = insert
s.length++
}
}
}
在指定结点后添加结点 func (s *SinglyLinkedList) InsertAfter(insert *Node,after *Node) {
currentNode := s.front
for currentNode != nil && currentNode != after && currentNode.next != nil {
currentNode = currentNode.next
}
if currentNode == after {
insert.next = after.next
after.next = insert
s.length++
}
}
删除指定结点 func (s *SinglyLinkedList) Remove(n *Node) {
if s.front == n {
s.front = n.next
s.length--
} else {
currentNode := s.front
// search for node n
for currentNode != nil && currentNode.next != nil && currentNode.next != n {
currentNode = currentNode.next
}
// see if current's next node is n
// if it's not n,then node n wasn't found in list s
if currentNode.next == n {
currentNode.next = currentNode.next.next
s.length--
}
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |