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

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

@@ -18,6 +18,7 @@ type ArrayList struct {
func assertImplementation() {
var _ list.IList = (*ArrayList)(nil)
var _ list.ILinkedList = (*ArrayList)(nil)
}
const (
@@ -208,3 +209,11 @@ func (l *ArrayList) Values() []interface{} {
copy(newElements, l.data[l.hidx+1:l.tidx])
return newElements
}
func (l *ArrayList) Traversal(every func(interface{}) bool) {
for i := uint(0); i < l.size; i++ {
if !every(l.data[i+l.hidx+1]) {
break
}
}
}

View File

@@ -215,6 +215,35 @@ func TestRemove(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 loadTestData() []int {
data, err := ioutil.ReadFile("../../l.log")
if err != nil {