golang实现常用数据结构

前端之家收集整理的这篇文章主要介绍了golang实现常用数据结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.数组栈的实现

package main

import (
"fmt"
)

type ItemType int

type Stack struct {
node [10]ItemType
maxsize int
top int
}

func StackInit(p *Stack) {
p.top = -1
StackCheckFull) bool {
if p>= p.maxsize return true
} else false
}
StackCheckEmpty<= 1 StackPush, v ItemType.top++
p.node[p] = v
return
StackPop(v {
v = p]
p--
return v
}

func main() var stack Stack
(&stack 100)
8844)
value := )
fmt.Println(value)
}

2.链表的实现

)

type item int

type list {
data item
next *list
}

var phead ptail *list
var listCount int

func main{
listInit()
listAdd()
listAdd9977)
listTraverse()
}

func listInit{
head := new(list)
phead = head
ptail = head
listCount = 0
}

func listAdd(data itemvar q *list )
q.nil
q.data = data
ptail= q
ptail = q
listCount++
}

func listInsert pos intint if pos >= listCount return 1
}
var ptmp = phead
for i 0; i < pos; i++ {
ptmp = ptmpnext
}
q = data
qnext
ptmp++
}

func listDelete(pos > listCount 1}
ptmpnextnext
}

func listTraversefor ptmp!= nil next
fmtPrintf"%d->" ptmp.data}

猜你在找的Go相关文章