package main
import (
"fmt"
"time"
)
type Woker struct {
Age int
Name string
}
type Node struct {
NextNode *Node
PreNode *Node
Data *Woker
}
//创建头结点
func CreateHeadNode(worker *Woker) *Node {
var headeNode = new(Node)
headeNode.NextNode = nil
headeNode.PreNode = nil
headeNode.Data = worker
return headeNode
}
//添加节点
func AddNode(node *Node, work *Woker) *Node {
var newNode = new(Node)
newNode.NextNode = nil
newNode.PreNode = nil
node.NextNode = newNode
newNode.Data = work
return newNode
}
func ShowNodes(node *Node) {
for {
fmt.Println(node.Data.Name, node.Data.Age)
time.Sleep(1 * time.Second)
if node.NextNode == nil {
break
} else {
node = node.NextNode
}
}
}
func main() {
fmt.Println("Hello World!")
var wk1 *Woker = new(Woker)
wk1.Age = 10
wk1.Name = "zhangsanfeng"
var headeNode = CreateHeadNode(wk1)
var wk2 *Woker = new(Woker)
wk2.Age = 10
wk2.Name = "zhangsanfeng2"
var node2 = AddNode(headeNode, wk2)
var wk3 *Woker = new(Woker)
wk3.Age = 10
wk3.Name = "zhangsanfeng3"
var node3 = AddNode(node2, wk3)
headeNode.PreNode = node3
node3.NextNode = headeNode
ShowNodes(headeNode)
}
链表-环形链表
最新推荐文章于 2023-10-16 00:52:12 发布