优先链表是否需要设计一种新的接口类型

This commit is contained in:
huangsimin
2019-07-25 11:40:50 +08:00
parent 7c4b358bd4
commit 849254edda
7 changed files with 270 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ package linkedlist
import (
"fmt"
"github.com/474420502/focus/list"
"github.com/davecgh/go-spew/spew"
)
@@ -22,6 +23,11 @@ type LinkedList struct {
size uint
}
func assertImplementation() {
var _ list.IList = (*LinkedList)(nil)
var _ list.ILinkedList = (*LinkedList)(nil)
}
func New() *LinkedList {
l := &LinkedList{}
l.head = &Node{}
@@ -44,8 +50,10 @@ func (l *LinkedList) CircularIterator() *CircularIterator {
}
func (l *LinkedList) Clear() {
l.head.next = nil
l.tail.prev = nil
l.head.next = l.tail
l.tail.prev = l.head
l.size = 0
}
@@ -448,6 +456,24 @@ TOPFOR:
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
}
func (l *LinkedList) Values() (result []interface{}) {
l.Traversal(func(value interface{}) bool {
result = append(result, value)

View File

@@ -3,7 +3,6 @@ package linkedlist
import (
"testing"
"github.com/Pallinder/go-randomdata"
"github.com/davecgh/go-spew/spew"
)
@@ -570,6 +569,35 @@ func TestRemoveIf2(t *testing.T) {
}
}
func TestTraversal(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
l.PushFront(uint(i))
}
var result []interface{}
l.Traversal(func(v interface{}) bool {
result = append(result, v)
return true
})
if spew.Sprint(result) != "[4 3 2 1 0]" {
t.Error(result)
}
l.PushBack(7, 8)
result = nil
l.Traversal(func(v interface{}) bool {
result = append(result, v)
return true
})
if spew.Sprint(result) != "[4 3 2 1 0 7 8]" {
t.Error(result)
}
}
func TestIterator(t *testing.T) {
ll := New()
for i := 0; i < 10; i++ {
@@ -639,46 +667,82 @@ func TestCircularIterator(t *testing.T) {
}
}
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 TestContains(t *testing.T) {
ll := New()
for i := 0; i < 10; i++ {
ll.Push(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)
for i := 0; i < 10; i++ {
if !ll.Contains(i) {
t.Error(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)
for i := 10; i < 20; i++ {
if ll.Contains(i) {
t.Error(i)
}
}
if v, _ := ll.Front(); v != 0 {
t.Error(v)
}
if v, _ := ll.Back(); v != 9 {
t.Error(v)
}
ll.Clear()
if !ll.Empty() {
t.Error("not Empty?")
}
if v, ok := ll.Front(); ok {
t.Error(v)
}
}
// 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)
// }
// }
// }