添加 比较获取值的方法 GetCompare...

This commit is contained in:
eson 2018-12-21 02:47:51 +08:00
parent a07c58bef4
commit c8a741ada7
2 changed files with 58 additions and 4 deletions

View File

@ -88,6 +88,52 @@ func (pl *PriorityList) Clear() {
pl.size = 0
}
// GetCompare 获取比较的节点 compare > 11这个节点小的值 [15,12,9,8,6,1] = 9
func (pl *PriorityList) GetCompare(cNode INode) INode {
cur := pl.head
for cur != nil {
if !cur.Compare(cNode) {
return cur
}
cur = cur.GetNext()
}
return nil
}
// GetCompareReverse 逆序获取比较的节点 compare > 11这个节点大的值 [15,12,9,8,6,1] = 12
func (pl *PriorityList) GetCompareReverse(cNode INode) INode {
cur := pl.tail
for cur != nil {
if cur.Compare(cNode) {
return cur
}
cur = cur.GetPrev()
}
return nil
}
// ForEach 顺序遍历
func (pl *PriorityList) ForEach(eachfunc func(INode) bool) {
cur := pl.head
for cur != nil {
if !eachfunc(cur) {
break
}
cur = cur.GetNext()
}
}
// ForEachReverse 逆遍历
func (pl *PriorityList) ForEachReverse(eachfunc func(INode) bool) {
cur := pl.tail
for cur != nil {
if !eachfunc(cur) {
break
}
cur = cur.GetPrev()
}
}
// Get 获取索引长度
func (pl *PriorityList) Get(idx int) INode {
if idx >= pl.size {

View File

@ -53,25 +53,33 @@ func TestPriority(t *testing.T) {
t.Error("Remove error current is ", pl.String(), pl.Size())
}
t.Log("size is", pl.Size())
size := pl.Size()
for i := 0; i < size; i++ {
pl.RemoveReverseIndex(0)
t.Log(i)
}
if pl.String() != "[]" || pl.Size() != 0 {
t.Error("Remove error current is ", pl.String(), pl.Size())
}
pl.Insert(NewNodeInt(6))
pl.Insert(NewNodeInt(5))
pl.Insert(NewNodeInt(7))
pl.Insert(NewNodeInt(1))
pl.Insert(NewNodeInt(9))
pl.Insert(NewNodeInt(15))
pl.Insert(NewNodeInt(12))
pl.Insert(NewNodeInt(8))
if pl.GetCompare(NewNodeInt(11)).GetValue() != 9 {
t.Error(pl.String(), pl.GetCompare(NewNodeInt(11)).GetValue())
}
if pl.GetCompareReverse(NewNodeInt(11)).GetValue() != 12 {
t.Error(pl.String(), pl.GetCompareReverse(NewNodeInt(11)).GetValue())
}
pl.Clear()
if pl.String() != "[]" || pl.Size() != 0 {
t.Error("Remove error current is ", pl.String(), pl.Size())
}
}