完善Remove

This commit is contained in:
huangsimin
2019-05-17 19:54:55 +08:00
parent b60d5b4f3d
commit 0230950382
5 changed files with 592 additions and 184 deletions

View File

@@ -1,6 +1,9 @@
package linkedlist
import "fmt"
type Node struct {
prev *Node
next *Node
value interface{}
}
@@ -11,97 +14,228 @@ func (node *Node) Value() interface{} {
type LinkedList struct {
head *Node
tail *Node
size uint
}
// var nodePool *sync.Pool = &sync.Pool{
// New: func() interface{} {
// return &Node{}
// },
// }
func New() *LinkedList {
return &LinkedList{}
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
}
func (l *LinkedList) Size() uint {
return l.size
}
func (l *LinkedList) Push(v interface{}) {
l.size++
if l.head == nil {
l.head = &Node{value: v}
return
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
}
l.head = &Node{value: v, next: l.head}
}
func (l *LinkedList) PushNode(n *Node) {
l.size++
if l.head == nil {
l.head = n
return
}
func (l *LinkedList) PushBack(values ...interface{}) {
n.next = l.head
l.head = n
var node *Node
l.size += uint(len(values))
for _, v := range values {
node = &Node{}
node.value = v
tprev := l.tail.prev
tprev.next = node
node.prev = tprev
node.next = l.tail
l.tail.prev = node
}
}
func (l *LinkedList) Pop() (result interface{}, found bool) {
if n, ok := l.PopNode(); ok {
return n.value, ok
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
}
return nil, false
}
func (l *LinkedList) PopNode() (result *Node, found bool) {
if l.head == nil {
return nil, false
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
}
return nil, false
}
func (l *LinkedList) Front() (result interface{}, found bool) {
if l.size != 0 {
return l.head.next.value, true
}
return nil, false
}
func (l *LinkedList) Back() (result interface{}, found bool) {
if l.size != 0 {
return l.tail.prev.value, true
}
return nil, false
}
func (l *LinkedList) Insert(idx uint, values ...interface{}) {
if idx > l.size {
return
}
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))
}
func (l *LinkedList) Remove(idx uint) {
if idx >= l.size {
panic(fmt.Sprintf("out of list range, size is %d, idx is %d", l.size, idx))
}
if l.head != nil {
if idx == 0 {
l.size--
temp := l.head
l.head = l.head.next
nodePool.Put(temp)
return
}
for cur := l.head; cur.next != nil; cur = cur.next {
if idx == 1 {
l.size--
result := cur.next
cur.next = result.next
result.next = nil
nodePool.Put(result)
return
}
idx--
}
}
result = l.head
found = true
l.head = result.next
result.next = nil
l.size--
return
}
func (l *LinkedList) Remove(idx uint) (result *Node, found bool) {
if l.size == 0 {
return nil, false
}
if idx == 0 {
result = l.head
found = true
l.head = result.next
result.next = nil
l.size--
return
}
for cur := l.head; cur.next != nil; cur = cur.next {
if idx == 1 {
l.size--
result = cur.next
found = true
cur.next = result.next
result.next = nil
return
}
idx--
}
return nil, false
}
func (l *LinkedList) Values() (result []interface{}) {
l.Traversal(func(cur *Node) bool {
result = append(result, cur.value)
l.Traversal(func(value interface{}) bool {
result = append(result, value)
return true
})
return
}
func (l *LinkedList) Traversal(every func(*Node) bool) {
for cur := l.head; cur != nil; cur = cur.next {
if !every(cur) {
func (l *LinkedList) Traversal(every func(interface{}) bool) {
for cur := l.head.next; cur != l.tail; cur = cur.next {
if !every(cur.value) {
break
}
}

View File

@@ -3,13 +3,14 @@ package linkedlist
import (
"testing"
"github.com/Pallinder/go-randomdata"
"github.com/davecgh/go-spew/spew"
)
func TestPush(t *testing.T) {
func TestPushFront(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
l.Push(i)
l.PushFront(i)
}
var result string
result = spew.Sprint(l.Values())
@@ -17,71 +18,219 @@ func TestPush(t *testing.T) {
t.Error(result)
}
l.Push(0)
l.PushFront(0)
result = spew.Sprint(l.Values())
if result != "[0 4 3 2 1 0]" {
t.Error(result)
}
}
func TestPop(t *testing.T) {
func TestPushBack(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
l.Push(i)
l.PushBack(i)
}
if v, ok := l.Pop(); ok {
if v != 4 {
t.Error(v)
}
} else {
t.Error("Pop should ok, but is not ok")
}
var result string
result = spew.Sprint(l.Values())
if result != "[3 2 1 0]" {
if result != "[0 1 2 3 4]" {
t.Error(result)
}
for i := 3; l.Size() != 0; i-- {
if v, ok := l.Pop(); ok {
if v != i {
t.Error(i, v, "is not equals")
}
} else {
t.Error("Pop should ok, but is not ok", i)
}
}
l.Push(0)
l.PushBack(0)
result = spew.Sprint(l.Values())
if result != "[0]" {
if result != "[0 1 2 3 4 0]" {
t.Error(result)
}
if l.Size() != 1 {
t.Error("l.Size() == 1, but is error, size = ", l.Size())
}
}
func TestRemove(t *testing.T) {
func TestPopFront(t *testing.T) {
l := New()
// "[0 1 2 3 4]"
for i := 0; i < 5; i++ {
l.Push(i)
l.PushFront(i)
}
// var result string
for i := 0; i < 5; i++ {
if n, ok := l.Remove(0); ok {
if n.Value() != 4-i {
t.Error(n)
for i := 4; i >= 0; i-- {
if v, ok := l.PopFront(); ok {
if v != i {
t.Error("[4 3 2 1 0] PopFront value should be ", i, ", but is ", v)
}
} else {
t.Error("Pop should ok, but is not ok", i)
t.Error("PopFront is not ok")
}
if l.Size() != uint(i) {
t.Error("l.Size() is error, is", l.Size())
}
}
}
func TestPopBack(t *testing.T) {
l := New()
// "[0 1 2 3 4]"
for i := 0; i < 5; i++ {
l.PushFront(i)
}
// var result string
for i := 0; i < 5; i++ {
if v, ok := l.PopBack(); ok {
if v != i {
t.Error("[4 3 2 1 0] PopFront value should be ", i, ", but is ", v)
}
} else {
t.Error("PopFront is not ok")
}
if l.Size() != uint(5-i-1) {
t.Error("l.Size() is error, is", l.Size())
}
}
if l.Size() != 0 {
t.Error("l.Size() == 0, but is error, size = ", l.Size())
}
func TestInsert(t *testing.T) {
l1 := New()
l2 := New()
// "[4 3 2 1 0]"
for i := 0; i < 5; i++ {
l1.Insert(0, i)
l2.PushFront(i)
}
var result1, result2 string
result1 = spew.Sprint(l1.Values())
result2 = spew.Sprint(l2.Values())
if result1 != result2 {
t.Error(result1, result2)
}
for i := 0; i < 5; i++ {
l1.Insert(l1.Size(), i)
l2.PushBack(i)
// t.Error(l1.Values(), l2.Values())
}
result1 = spew.Sprint(l1.Values())
result2 = spew.Sprint(l2.Values())
if result1 != result2 {
t.Error(result1, result2)
}
if result1 != "[4 3 2 1 0 0 1 2 3 4]" {
t.Error("result should be [4 3 2 1 0 0 1 2 3 4]\n but result is", result1)
}
l1.Insert(1, 99)
result1 = spew.Sprint(l1.Values())
if result1 != "[4 99 3 2 1 0 0 1 2 3 4]" {
t.Error("[4 3 2 1 0 0 1 2 3 4] insert with index 1, should be [4 99 3 2 1 0 0 1 2 3 4]\n but result is", result1)
}
}
func BenchmarkPushBack(b *testing.B) {
ec := 5
cs := 2000000
b.N = cs * ec
for c := 0; c < ec; c++ {
l := New()
for i := 0; i < cs; i++ {
l.PushBack(i)
}
}
}
func BenchmarkPushFront(b *testing.B) {
ec := 5
cs := 2000000
b.N = cs * ec
for c := 0; c < ec; c++ {
l := New()
for i := 0; i < cs; i++ {
l.PushFront(i)
}
}
}
func BenchmarkInsert(b *testing.B) {
ec := 10
cs := 1000
b.N = cs * ec
for c := 0; c < ec; c++ {
l := New()
for i := 0; i < cs; i++ {
ridx := randomdata.Number(0, int(l.Size())+1)
l.Insert(uint(ridx), i)
}
}
}
// func TestPop(t *testing.T) {
// l := New()
// for i := 0; i < 5; i++ {
// l.Push(i)
// }
// if v, ok := l.Pop(); ok {
// if v != 4 {
// t.Error(v)
// }
// } else {
// t.Error("Pop should ok, but is not ok")
// }
// var result string
// result = spew.Sprint(l.Values())
// if result != "[3 2 1 0]" {
// t.Error(result)
// }
// for i := 3; l.Size() != 0; i-- {
// if v, ok := l.Pop(); ok {
// if v != i {
// t.Error(i, v, "is not equals")
// }
// } else {
// t.Error("Pop should ok, but is not ok", i)
// }
// }
// l.Push(0)
// result = spew.Sprint(l.Values())
// if result != "[0]" {
// t.Error(result)
// }
// if l.Size() != 1 {
// t.Error("l.Size() == 1, but is error, size = ", l.Size())
// }
// }
// func TestRemove(t *testing.T) {
// l := New()
// for i := 0; i < 5; i++ {
// l.Push(i)
// }
// for i := 0; i < 5; i++ {
// l.Remove(0)
// if l.head != nil {
// if l.head.Value() != 4-i-1 {
// t.Error("l.head is error")
// }
// }
// t.Error(l.Size())
// }
// if l.Size() != 0 {
// t.Error("l.Size() == 0, but is error, size = ", l.Size())
// }
// }