focus/list/linked_list/linked_list.go

496 lines
9.1 KiB
Go
Raw Normal View History

2019-05-10 10:01:15 +00:00
package linkedlist
2019-07-19 09:41:28 +00:00
import (
"fmt"
"github.com/474420502/focus/list"
2019-07-19 09:41:28 +00:00
"github.com/davecgh/go-spew/spew"
)
2019-05-17 11:54:55 +00:00
2019-05-10 10:01:15 +00:00
type Node struct {
2019-05-17 11:54:55 +00:00
prev *Node
2019-05-10 10:01:15 +00:00
next *Node
value interface{}
}
func (node *Node) Value() interface{} {
return node.value
}
type LinkedList struct {
head *Node
2019-05-17 11:54:55 +00:00
tail *Node
2019-05-10 10:01:15 +00:00
size uint
}
func assertImplementation() {
var _ list.IList = (*LinkedList)(nil)
var _ list.ILinkedList = (*LinkedList)(nil)
}
2019-05-10 10:01:15 +00:00
func New() *LinkedList {
2019-05-17 11:54:55 +00:00
l := &LinkedList{}
l.head = &Node{}
l.head.prev = nil
l.tail = &Node{}
l.tail.next = nil
l.head.next = l.tail
l.tail.prev = l.head
return l
2019-05-10 10:01:15 +00:00
}
2019-07-19 09:41:28 +00:00
func (l *LinkedList) Iterator() *Iterator {
return &Iterator{ll: l, cur: l.head}
}
func (l *LinkedList) CircularIterator() *CircularIterator {
return &CircularIterator{pl: l, cur: l.head}
}
2019-05-22 09:52:50 +00:00
func (l *LinkedList) Clear() {
l.head.next = l.tail
l.tail.prev = l.head
2019-05-22 09:52:50 +00:00
l.size = 0
}
func (l *LinkedList) Empty() bool {
return l.size == 0
}
2019-05-10 10:01:15 +00:00
func (l *LinkedList) Size() uint {
return l.size
}
func (l *LinkedList) Push(value interface{}) {
var node *Node
l.size++
node = &Node{}
node.value = value
tprev := l.tail.prev
tprev.next = node
node.prev = tprev
node.next = l.tail
l.tail.prev = node
}
2019-05-17 11:54:55 +00:00
func (l *LinkedList) PushFront(values ...interface{}) {
var node *Node
l.size += uint(len(values))
for _, v := range values {
node = &Node{}
node.value = v
hnext := l.head.next
hnext.prev = node
node.next = hnext
node.prev = l.head
l.head.next = node
2019-05-10 10:01:15 +00:00
}
}
2019-05-17 11:54:55 +00:00
func (l *LinkedList) PushBack(values ...interface{}) {
var node *Node
l.size += uint(len(values))
for _, v := range values {
node = &Node{}
node.value = v
tprev := l.tail.prev
tprev.next = node
2019-05-10 10:01:15 +00:00
2019-05-17 11:54:55 +00:00
node.prev = tprev
node.next = l.tail
l.tail.prev = node
}
2019-05-10 10:01:15 +00:00
}
2019-05-17 11:54:55 +00:00
func (l *LinkedList) PopFront() (result interface{}, found bool) {
if l.size != 0 {
l.size--
temp := l.head.next
hnext := temp.next
hnext.prev = l.head
l.head.next = hnext
result = temp.value
found = true
return
2019-05-10 10:01:15 +00:00
}
return nil, false
}
2019-05-17 11:54:55 +00:00
func (l *LinkedList) PopBack() (result interface{}, found bool) {
if l.size != 0 {
l.size--
temp := l.tail.prev
tprev := temp.prev
tprev.next = l.tail
l.tail.prev = tprev
result = temp.value
found = true
return
2019-05-10 10:01:15 +00:00
}
2019-05-17 11:54:55 +00:00
return nil, false
}
2019-05-10 10:01:15 +00:00
2019-05-17 11:54:55 +00:00
func (l *LinkedList) Front() (result interface{}, found bool) {
if l.size != 0 {
return l.head.next.value, true
}
return nil, false
2019-05-10 10:01:15 +00:00
}
2019-05-17 11:54:55 +00:00
func (l *LinkedList) Back() (result interface{}, found bool) {
if l.size != 0 {
return l.tail.prev.value, true
2019-05-10 10:01:15 +00:00
}
2019-05-17 11:54:55 +00:00
return nil, false
}
2019-05-10 10:01:15 +00:00
2019-05-21 06:28:08 +00:00
func (l *LinkedList) Find(every func(idx uint, value interface{}) bool) (interface{}, bool) {
idx := uint(0)
// 头部
for cur := l.head.next; cur != l.tail; cur = cur.next {
if every(idx, cur.value) {
return cur.value, true
}
idx++
}
return nil, false
}
func (l *LinkedList) FindMany(every func(idx uint, value interface{}) int) (result []interface{}, isfound bool) {
// the default of isfould is false
idx := uint(0)
// 头部
for cur := l.head.next; cur != l.tail; cur = cur.next {
j := every(idx, cur.value)
switch {
case j > 0:
result = append(result, cur.value)
isfound = true
case j < 0:
return result, isfound
}
idx++
}
return result, isfound
}
2019-05-19 18:14:46 +00:00
func (l *LinkedList) Index(idx uint) (interface{}, bool) {
if idx >= l.size {
return nil, false
}
if idx > l.size/2 {
idx = l.size - 1 - idx
// 尾部
2019-05-21 06:28:08 +00:00
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
2019-05-19 18:14:46 +00:00
if idx == 0 {
return cur.value, true
}
idx--
}
} else {
// 头部
2019-05-21 06:28:08 +00:00
for cur := l.head.next; cur != l.tail; cur = cur.next {
2019-05-19 18:14:46 +00:00
if idx == 0 {
return cur.value, true
}
idx--
}
}
return nil, false
}
2019-07-19 11:05:35 +00:00
func (l *LinkedList) Insert(idx uint, values ...interface{}) bool {
2019-07-19 09:41:28 +00:00
if idx > l.size { // 插入的方式 可能导致size的范围判断不一样
2019-07-19 11:05:35 +00:00
return false
2019-05-10 10:01:15 +00:00
}
2019-05-17 11:54:55 +00:00
if idx > l.size/2 {
idx = l.size - idx
// 尾部
for cur := l.tail.prev; cur != nil; cur = cur.prev {
if idx == 0 {
var start *Node
var end *Node
start = &Node{value: values[0]}
end = start
for _, value := range values[1:] {
node := &Node{value: value}
end.next = node
node.prev = end
end = node
}
cnext := cur.next
cur.next = start
start.prev = cur
end.next = cnext
cnext.prev = end
break
}
idx--
}
} else {
// 头部
for cur := l.head.next; cur != nil; cur = cur.next {
if idx == 0 {
var start *Node
var end *Node
start = &Node{value: values[0]}
end = start
for _, value := range values[1:] {
node := &Node{value: value}
end.next = node
node.prev = end
end = node
}
cprev := cur.prev
cprev.next = start
start.prev = cprev
end.next = cur
cur.prev = end
break
}
idx--
}
}
l.size += uint(len(values))
2019-07-19 11:05:35 +00:00
return true
2019-05-17 11:54:55 +00:00
}
2019-07-19 09:41:28 +00:00
// InsertState InsertIf的every函数的枚举 从左到右 1 为前 2 为后 insert here(2) ->cur-> insert here(1)
// UninsertAndContinue 不插入并且继续
// UninsertAndBreak 不插入并且停止
// InsertBack cur后插入并且停止
// InsertFront cur前插入并且停止
type InsertState int
const (
// UninsertAndContinue 不插入并且继续
UninsertAndContinue InsertState = 0
// UninsertAndBreak 不插入并且停止
UninsertAndBreak InsertState = -1
// InsertBack cur后插入并且停止
InsertBack InsertState = 2
// InsertFront cur前插入并且停止
InsertFront InsertState = 1
)
// InsertIf every函数的枚举 从左到右遍历 1 为前 2 为后 insert here(2) ->cur-> insert here(1)
func (l *LinkedList) InsertIf(every func(idx uint, value interface{}) InsertState, values ...interface{}) {
2019-05-19 18:14:46 +00:00
idx := uint(0)
// 头部
for cur := l.head.next; cur != nil; cur = cur.next {
2019-07-19 09:41:28 +00:00
insertState := every(idx, cur.value)
if insertState == UninsertAndContinue {
continue
}
if insertState > 0 { // 1 为前 2 为后 insert here(2) ->cur-> insert here(1)
2019-05-19 18:14:46 +00:00
var start *Node
var end *Node
start = &Node{value: values[0]}
end = start
for _, value := range values[1:] {
node := &Node{value: value}
end.next = node
node.prev = end
end = node
}
2019-07-19 09:41:28 +00:00
if insertState == InsertBack {
cprev := cur.prev
cprev.next = start
start.prev = cprev
end.next = cur
cur.prev = end
2019-07-19 09:41:28 +00:00
} else { // InsertFront
cnext := cur.next
cnext.prev = end
start.prev = cur
cur.next = start
end.next = cnext
}
2019-05-19 18:14:46 +00:00
2019-05-21 06:28:08 +00:00
l.size += uint(len(values))
2019-07-19 09:41:28 +00:00
return
2019-05-19 18:14:46 +00:00
}
2019-07-19 09:41:28 +00:00
// 必然 等于 UninsertAndBreak
return
2019-05-19 18:14:46 +00:00
}
}
func remove(cur *Node) {
curPrev := cur.prev
curNext := cur.next
curPrev.next = curNext
curNext.prev = curPrev
cur.prev = nil
cur.next = nil
}
2019-05-19 18:14:46 +00:00
func (l *LinkedList) Remove(idx uint) (interface{}, bool) {
2019-07-19 09:41:28 +00:00
if l.size <= idx {
2019-07-19 11:05:35 +00:00
// log.Printf("out of list range, size is %d, idx is %d\n", l.size, idx)
2019-07-19 09:41:28 +00:00
return nil, false
2019-05-17 11:54:55 +00:00
}
2019-05-19 18:14:46 +00:00
2019-05-20 11:12:38 +00:00
l.size--
2019-05-19 18:14:46 +00:00
if idx > l.size/2 {
idx = l.size - idx // l.size - 1 - idx, 先减size
2019-05-19 18:14:46 +00:00
// 尾部
2019-05-21 06:28:08 +00:00
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
2019-05-19 18:14:46 +00:00
if idx == 0 {
remove(cur)
2019-05-19 18:14:46 +00:00
return cur.value, true
}
idx--
2019-05-10 10:01:15 +00:00
}
2019-05-17 11:54:55 +00:00
2019-05-19 18:14:46 +00:00
} else {
// 头部
2019-05-21 06:28:08 +00:00
for cur := l.head.next; cur != l.tail; cur = cur.next {
2019-05-19 18:14:46 +00:00
if idx == 0 {
remove(cur)
2019-05-19 18:14:46 +00:00
return cur.value, true
2019-05-17 11:54:55 +00:00
}
idx--
}
2019-05-10 10:01:15 +00:00
}
2019-05-19 18:14:46 +00:00
panic(fmt.Sprintf("unknown error"))
2019-05-10 10:01:15 +00:00
}
2019-07-19 09:41:28 +00:00
// RemoveState RemoveIf的every函数的枚举
// RemoveAndContinue 删除并且继续
// RemoveAndBreak 删除并且停止
// UnremoveAndBreak 不删除并且停止遍历
// UnremoveAndContinue 不删除并且继续遍历
type RemoveState int
const (
// RemoveAndContinue 删除并且继续
RemoveAndContinue RemoveState = iota
// RemoveAndBreak 删除并且停止
RemoveAndBreak
// UnremoveAndBreak 不删除并且停止遍历
UnremoveAndBreak
// UnremoveAndContinue 不删除并且继续遍历
UnremoveAndContinue
)
// RemoveIf every的遍历函数操作remove过程 如果没删除result 返回nil, isRemoved = false
func (l *LinkedList) RemoveIf(every func(idx uint, value interface{}) RemoveState) (result []interface{}, isRemoved bool) {
// 头部
2019-05-21 06:28:08 +00:00
idx := uint(0)
2019-07-19 09:41:28 +00:00
TOPFOR:
2019-05-21 06:28:08 +00:00
for cur := l.head.next; cur != l.tail; idx++ {
2019-07-19 09:41:28 +00:00
removeState := every(idx, cur.value)
switch removeState {
case RemoveAndContinue:
result = append(result, cur.value)
isRemoved = true
temp := cur.next
remove(cur)
cur = temp
l.size--
continue TOPFOR
case RemoveAndBreak:
2019-05-21 06:28:08 +00:00
result = append(result, cur.value)
isRemoved = true
temp := cur.next
remove(cur)
2019-05-21 06:28:08 +00:00
cur = temp
l.size--
2019-07-19 09:41:28 +00:00
return
case UnremoveAndContinue:
case UnremoveAndBreak:
2019-05-21 06:28:08 +00:00
return
}
2019-05-21 06:28:08 +00:00
cur = cur.next
}
return
}
func (l *LinkedList) Contains(values ...interface{}) bool {
for _, searchValue := range values {
found := false
for cur := l.head.next; cur != l.tail; cur = cur.next {
if cur.value == searchValue {
found = true
break
}
}
if !found {
return false
}
}
return true
}
2019-05-10 10:01:15 +00:00
func (l *LinkedList) Values() (result []interface{}) {
2019-05-17 11:54:55 +00:00
l.Traversal(func(value interface{}) bool {
result = append(result, value)
2019-05-10 10:01:15 +00:00
return true
})
return
}
2019-07-19 09:41:28 +00:00
func (l *LinkedList) String() string {
return spew.Sprint(l.Values())
}
2019-05-17 11:54:55 +00:00
func (l *LinkedList) Traversal(every func(interface{}) bool) {
for cur := l.head.next; cur != l.tail; cur = cur.next {
if !every(cur.value) {
2019-05-10 10:01:15 +00:00
break
}
}
}