首页 > 其他 > 详细

Golang之实现(链表)

时间:2018-01-14 21:54:20      阅读:175      评论:0      收藏:0      [点我收藏+]

链表算法

技术分享图片
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
    }
}
Link.go
技术分享图片
package main

import "fmt"

func main() {

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

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

 

Golang之实现(链表)

原文:https://www.cnblogs.com/pyyu/p/8284336.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!