您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Golang之实现(链表)

Golang之实现(链表)

来源:伴沃教育
package main

import "fmt"

type LinkNode struct {
    data interface{}
    next *LinkNode
}
type Link struct {
    head *LinkNode
    tail *LinkNode
}

func (p *Link) InsertHead(data interface{}) {
    node := &LinkNode{
        data: data,
        next: nil,
    }
    if p.tail == nil && p.head == nil {
        p.tail = node
        p.head = node
        return
    }
}

func (p *Link) InsertTail(data interface{}) {
    node := &LinkNode{
        data: data,
        next: nil,
    }
    if p.tail == nil && p.head == nil {
        p.tail = node
        p.head = node
        return
    }
    p.tail.next = node
    p.tail = node
}
func (p *Link)Trans(){
    q:=p.head
    for q!=nil{
        fmt.Println(q.data)
        q=q.next
    }
}

func main() {

    var link Link
    for i := 0; i < 10; i++ {

        //link.InsertHead(i)
        link.InsertTail(fmt.Sprintf("str %d",i))
    }
    link.Trans()
}

转载于:https://www.cnblogs.com/nyist-xsk/p/11352298.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务