问题:
实现一个双链表
代码示例:
package main
import (
"fmt"
)
// 双链表结点
type RNode struct {
data interface{}
prev *RNode
next *RNode
}
func main() {
// 双链表
DlistHead, DlistTail := getDList()
// 前向后遍历
tmpDlist := DlistHead
for tmpDlist != nil {
fmt.Println(tmpDlist.data)
tmpDlist = tmpDlist.next
}
// 后向前遍历
tmpDlist1 := DlistTail
for tmpDlist1 != nil {
fmt.Println(tmpDlist1.data)
tmpDlist1 = tmpDlist1.prev
}
}
func getDList() (head, tail *RNode) {
node := new(RNode)
node.data = 1
head = node
for i:=2; i<=9; i++ {
temp := new(RNode)
temp.data = i
temp.prev = node
node.next = temp
node = temp
}
return head, node
}