59 lines
912 B
Go
59 lines
912 B
Go
package plist
|
|
|
|
import (
|
|
"github.com/davecgh/go-spew/spew"
|
|
)
|
|
|
|
// Node 节点结构
|
|
type Node struct {
|
|
next *Node
|
|
value interface{}
|
|
}
|
|
|
|
// PriorityList
|
|
type PriorityList struct {
|
|
size int
|
|
head *Node
|
|
}
|
|
|
|
// New a node
|
|
func New() *PriorityList {
|
|
p := new(PriorityList)
|
|
return p
|
|
}
|
|
|
|
// String 展示需要的
|
|
func (pl *PriorityList) String() string {
|
|
|
|
content := ""
|
|
|
|
for cur := pl.head; cur != nil; cur = cur.next {
|
|
content += spew.Sprint(cur.value) + "-"
|
|
}
|
|
|
|
if content != "" && content[len(content)-1] == '-' {
|
|
content = content[:len(content)-1]
|
|
}
|
|
|
|
return content
|
|
}
|
|
|
|
// insert 插入值
|
|
func (pl *PriorityList) nodeInsert(cur *Node, value interface{}) {
|
|
if cur == nil {
|
|
temp := pl.head
|
|
pl.head = &Node{value: value}
|
|
pl.head.next = temp
|
|
return
|
|
}
|
|
|
|
temp := cur.next
|
|
cur.next = &Node{value: value}
|
|
cur.next.next = temp
|
|
}
|
|
|
|
// Size 长度
|
|
func (pl *PriorityList) Size() int {
|
|
return pl.size
|
|
}
|