2019独角兽企业重金招聘Python工程师标准>>>
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
}
func StackCheckFull(p *Stack) bool {
if p.top >= p.maxsize {
return true
} else {
return false
}
}
func StackCheckEmpty(p *Stack) bool {
if p.top <= -1 {
return true
} else {
return false
}
}
func StackPush(p *Stack, v ItemType) {
p.top++
p.node[p.top] = v
return
}
func StackPop(p *Stack) (v ItemType) {
v = p.node[p.top]
p.top--
return v
}
func main() {
var stack Stack
StackPush(&stack, 100)
StackPush(&stack, 88)
StackPush(&stack, 44)
value := StackPop(&stack)
fmt.Println(value)
}
2.链表的实现
package main
import (
"fmt"
)
type item int
type list struct {
data item
next *list
}
var phead, ptail *list
var listCount int
func main() {
listInit()
listAdd(100)
listAdd(99)
listAdd(77)
listTraverse()
}
func listInit() {
head := new(list)
phead = head
ptail = head
listCount = 0
}
func listAdd(data item) {
var q *list = new(list)
q.next = nil
q.data = data
ptail.next = q
ptail = q
listCount++
}
func listInsert(data item, pos int) int {
if pos >= listCount {
return -1
}
var ptmp *list = phead
for i := 0; i < pos; i++ {
ptmp = ptmp.next
}
q := new(list)
q.data = data
q.next = ptmp.next
ptmp.next = q
listCount++
return 0
}
func listDelete(pos int) int {
if pos > listCount {
return -1
}
var ptmp *list = phead
for i := 0; i < pos-1; i++ {
ptmp = ptmp.next
}
ptmp.next = ptmp.next.next
return 0
}
func listTraverse() {
var ptmp *list = phead
for ptmp.next != nil {
ptmp = ptmp.next
fmt.Printf("%d->", ptmp.data)
}
}