刘三羊

区块链技术老白一枚

链表-环形链表
    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)
    }
阅读更多
文章标签: 环形链表
个人分类: 密码学
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

链表-环形链表

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭