var table *jobScheduleTable
type jobNode struct {
forward []*jobNode
data int
}
type jobScheduleTable struct {
head *jobNode
level int
}
func initJobSchelduleTable() {
table = &jobScheduleTable{
head: &jobNode{forward: make([]*jobNode, MaxLevel)},
level: 1,
}
}
func newJobNode(data int, level int) *jobNode {
return &jobNode{forward: make([]*jobNode, level), data: data}
}
func (t *jobScheduleTable) Insert(data int) {
level := randLevel()
if level > t.level {
level = t.level + 1
t.level = level
}
current := t.head
before := make([]*jobNode, level)
after := make([]*jobNode, level)
for i := level; i >= 1; i-- {
if current.forward[i-1] == nil || current.forward[i-1].data > data {
after[i-1] = current.forward[i-1]
before[i-1] = current
} else {
for current.forward[i-1] != nil && current.forward[i-1].data < data {
current = current.forward[i-1]
}
before[i-1] = current
after[i-1] = current.forward[i-1]
}
}
node := newJobNode(data, level)
for i := 0; i < level; i++ {
node.forward[i] = after[i]
before[i].forward[i] = node
}
}
func (t *jobScheduleTable) Search(data int) {
current := t.head
for i := t.level; i >= 1; i-- {
if current.forward[i-1] == nil || current.forward[i-1].data > data {
continue
} else {
for current.forward[i-1] != nil && current.forward[i-1].data < data {
current = current.forward[i-1]
fmt.Println(current.data)
}
if current.forward[i-1] != nil {
if current.forward[i-1].data == data {
fmt.Println(data, i)
return
}
}
}
}
}
func(t *jobScheduleTable)Delete(data int){
current := t.head
for i:= t.level;i>=1;i-- {
for current.forward[i-1] != nil {
if current.forward[i-1].data == data{
tmp := current.forward[i-1]
current.forward[i-1] = tmp.forward[i-1]
tmp.forward[i-1] = nil
break
}else if current.forward[i-1].data > data{
break
}else {
current = current.forward[i-1]
}
}
}
}
func(t *jobScheduleTable)Pop()int{
d := t.head.forward[0].data
t.Delete(d)
return d
}
func (t *jobScheduleTable) Print() {
for i := t.level; i >= 1; i-- {
current := t.head
for {
if current.forward[i-1] == nil {
break
}
fmt.Printf("%d ", current.forward[i-1].data)
current = current.forward[i-1]
}
fmt.Printf("***************** Level %d \n", i)
}
}
func randLevel() int {
rand.Seed(time.Now().UnixNano())
var level = 1
for {
if rand.Float64() > 0.25 || level >= MaxLevel {
break
}
level++
}
return level
}
go 跳表实现
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...
- 二分法查找需要是有序的数据,并且数据是放在数组当中。链表是否能实现二分查找呢?根据定义是无法实现的。我们可以借助其...
- 跳表(skip list)java实现 WiKi 【转载】http://blog.csdn.net/brillia...