码迷,mamicode.com
首页 > 其他好文 > 详细

Golang之实现(链表)

时间:2018-01-14 22:50:30      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:gif   splay   str   closed   pos   inter   分享图片   style   bsp   

链表算法

技术分享图片
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之实现(链表)

标签:gif   splay   str   closed   pos   inter   分享图片   style   bsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!