链表-环形链表
package mainimport ("fmt""time")type Woker struct {Age intName string}type Node struct {NextNode *NodePreNode *NodeData *Woker}//創(chuàng)建頭結(jié)點func CreateHeadNode(worker *Woker) *Node {var headeNode = new(Node)headeNode.NextNode = nilheadeNode.PreNode = nilheadeNode.Data = workerreturn headeNode}//添加節(jié)點func AddNode(node *Node, work *Woker) *Node {var newNode = new(Node)newNode.NextNode = nilnewNode.PreNode = nilnode.NextNode = newNodenewNode.Data = workreturn 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 = 10wk1.Name = "zhangsanfeng"var headeNode = CreateHeadNode(wk1)var wk2 *Woker = new(Woker)wk2.Age = 10wk2.Name = "zhangsanfeng2"var node2 = AddNode(headeNode, wk2)var wk3 *Woker = new(Woker)wk3.Age = 10wk3.Name = "zhangsanfeng3"var node3 = AddNode(node2, wk3)headeNode.PreNode = node3node3.NextNode = headeNodeShowNodes(headeNode)}
總結(jié)