From ad18004bc236e0dd59dceca521b11663d867753f Mon Sep 17 00:00:00 2001 From: huangsimin Date: Sun, 27 Jan 2019 18:42:08 +0800 Subject: [PATCH] TODO: level find --- priority_queue/priority_list.go | 73 +++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/priority_queue/priority_list.go b/priority_queue/priority_list.go index 09fbd4f..d1e7389 100644 --- a/priority_queue/priority_list.go +++ b/priority_queue/priority_list.go @@ -27,8 +27,9 @@ type LevelSize struct { // LevelNode 层级列表 type LevelNode struct { - head *LevelNode - tail *LevelNode + node *Node + prev *LevelNode + next *LevelNode sub interface{} lsize *LevelSize level int @@ -39,9 +40,14 @@ func New() *PriorityList { p := new(PriorityList) p.level = new(LevelNode) p.level.lsize = new(LevelSize) + p.level.lsize.size = 1 + + lnode := new(LevelNode) + lnode.lsize = new(LevelSize) + p.level.sub = lnode node := new(Node) - p.level.sub = node + lnode.sub = node return p } @@ -57,12 +63,22 @@ func (pl *PriorityList) String() string { // InsertValues 插入值 func (pl *PriorityList) InsertValues(values ...interface{}) { - ll := pl.level - for _, value := range values { node := new(Node) node.IValue = value + ll := pl.level + // 寻找 适合的level + for ll.level != 0 { + + if pl.compare(node, ll.node) > 0 { + temp := ll.prev + + } + + } + + // 找到数据后 if ll.level == 0 { if ll.sub == nil { @@ -71,40 +87,37 @@ func (pl *PriorityList) InsertValues(values ...interface{}) { } cur := ll.sub.(*Node) - cur. - } - if level.tail == nil { - level.head.lsize - node.prev = level.head - level.tail = node - continue - } + isTail := false - cur := level.head - for cur != nil { + for i := 0; i < ll.lsize.size; i++ { + if pl.compare(node, cur) > 0 { - if pl.compare(cur, node) < 0 { - - if cur.sub != nil { - cur = cur.sub - continue - } else { - temp := cur.next - cur.next = node + temp := cur.prev + cur.prev = node if temp != nil { - node.next = temp - temp.prev = node + node.prev = temp + temp.next = node } + ll.lsize.size++ pl.size++ - break + + isTail = true } - - } else { - + cur = cur.next + } + + if isTail { + temp := cur.next + cur.next = node + if temp != nil { + node.next = temp + temp.prev = node + } + ll.lsize.size++ + pl.size++ } - cur = cur.next } } }