首页 > 其他 > 详细

【数据结构】链表的基本学习 go实现版

时间:2019-06-02 19:54:25      阅读:101      评论:0      收藏:0      [点我收藏+]
package test;

import (
    "testing"
    "fmt"
)

type InteegetCell struct{
    Value int
    Next *InteegetCell
}

// 初始化链表(建立一个头指针)
func initCell() *InteegetCell{
    return new(InteegetCell)
}

// 头插法追加元素
func AddAtBeginning(top *InteegetCell, value int){
    newCell := &InteegetCell{value,top.Next}
    top.Next = newCell
}

// 尾插法的实现
func AddAtTali(top *InteegetCell, value int){
    for top.Next != nil {
        top = top.Next
    }
    newCell := &InteegetCell{value,nil}
    top.Next = newCell
}

// 变历链表
func Interte(top *InteegetCell){
    var result []int
    for top != nil {
        value := top.Value
        result = append(result, value)
        top = top.Next
    }   
    fmt.Println(result) 
    
}



func Test(t *testing.T){
    top := initCell()

    // fmt.Println("-----测试头插法-----")
    // for i:=1; i<4; i++ {
    //  AddAtBeginning(top,i)
    // }
    // // 遍历链表
    // Interte(top)

    fmt.Println("-----测试尾插法-----")
    AddAtTali(top,1)
    AddAtTali(top,2)
    AddAtTali(top,3)
    Interte(top)

    

}


【数据结构】链表的基本学习 go实现版

原文:https://www.cnblogs.com/jzsg/p/10963916.html

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