首页 > 其他分享 >go语言 单向链表

go语言 单向链表

时间:2022-08-20 13:22:05浏览次数:56  
标签:node head 单向 next 链表 go data 节点

//示例45 package main
import "fmt"
func main() {   var intlink Link   for i := 0; i < 10; i++ {     intlink.InsertTail(i)   }   intlink.Trans() }
//节点 type LinkNode struct {   data interface{} //内容   next *LinkNode //next指针 }
//链表 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   }   //如果不是第一个节点 新节点的next是链表的头节点   node.next = p.head   //链表头节点指向新节点   p.head = node
}
//尾插法 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   }
  //链表的尾节点的next指针指向新节点   p.tail.next = node   //链表的尾节点指向新节点   p.tail = node
}
//遍历 func (p *Link) Trans() { q := p.head for q != nil { fmt.Println(q.data) q = q.next //移动指针 } }

标签:node,head,单向,next,链表,go,data,节点
From: https://www.cnblogs.com/chenweihao/p/16607570.html

相关文章

  • Golang基础教程
    以下使用goland的IDE演示,包含总计的golang基础功能共20个章节一、go语言结构:二、go基础语法:三、变量四、常量五、运算符六、条件语句七、循环八、函数九、变量......
  • django中的中间件
    1.什么是中间件中间件是django的门户,在请求响应进入进出django的时候,都需要先经过中间件,用来全局改变django的输入和输出。django中自带7个中间件,每个中间件都有其特定......
  • 解决goland在mac m1下无法调试问题
     背景新电脑macm1goland调试抛出异常   异常信息第一次异常信息couldnotlaunchprocess:cannotrununderRosetta,checkthattheinstalledbuildo......
  • 代码审计(Java)——WebGoat_AuthenticationFlaws
     零、SecurePassword这里没什么可审计的,经典的爆破,定期更换复杂度相当的密码吧……一、Passwordreset1.level2这里题目给出的信息是登录自己的WebWolf......
  • go 开篇
    一件事情仅有一种做法的理念Go坚持“一件事情仅有一种做法的理念”,只保留了for这一种循环结构,去掉了C语言中的while和do-while循环结构;Go填平了C语言中swit......
  • go if语法
    goif语句自身的特点和Go函数一样,if语句的分支代码块的左大括号与if关键字在同一行上,这也是Go代码风格的统一要求,gofmt工具会帮助我们实现这一点;if语句的布尔表......
  • go学习框架
    基本语法,学+练安装编译及运行demo构建模式及演化gomodule的常规操作入口文件及包初始化语言类型变量常量数组和切片 ......
  • [Google] LeetCode 366 Find Leaves of Binary Tree
    Giventherootofabinarytree,collectatree'snodesasifyouweredoingthis:Collectalltheleafnodes.Removealltheleafnodes.Repeatuntilthetre......
  • [Google] LeetCode 2096 Step-By-Step Directions From a Binary Tree Node to Anothe
    Youaregiventherootofabinarytreewithnnodes.Eachnodeisuniquelyassignedavaluefrom1ton.YouarealsogivenanintegerstartValuerepresenting......
  • golang中GOPATH、GOROOT、GOBIN不生效等相关问题
    比较重要的三个配置:GOPATH、GOROOT、GOBINGOPATH:go项目开发的工程目录GOROOT:go安装所在的目录GOBIN:go项目编译完二进制程序目录不生效问题,其实应该好好检查是否......