Go语言单链表实现方法
作者:OSC首席键客 时间:2024-02-14 15:46:45
本文实例讲述了Go语言单链表实现方法。分享给大家供大家参考。具体如下:
1. singlechain.go代码如下:
//////////
//单链表 -- 线性表
package singlechain
//定义节点
type Node struct {
Data int
Next *Node
}
/*
* 返回第一个节点
* h 头结点
*/
func GetFirst(h *Node) *Node {
if h.Next == nil {
return nil
}
return h.Next
}
/*
* 返回最后一个节点
* h 头结点
*/
func GetLast(h *Node) *Node {
if h.Next == nil {
return nil
}
i := h
for i.Next != nil {
i = i.Next
if i.Next == nil {
return i
}
}
return nil
}
//取长度
func GetLength(h *Node) int {
var i int = 0
n := h
for n.Next != nil {
i++
n = n.Next
}
return i
}
//插入一个节点
//h: 头结点
//d:要插入的节点
//p:要插入的位置
func Insert(h, d *Node, p int) bool {
if h.Next == nil {
h.Next = d
return true
}
i := 0
n := h
for n.Next != nil {
i++
if i == p {
if n.Next.Next == nil {
n.Next = d
return true
} else {
d.Next = n.Next
n.Next = d.Next
return true
}
}
n = n.Next
if n.Next == nil {
n.Next = d
return true
}
}
return false
}
//取出指定节点
func GetLoc(h *Node, p int) *Node {
if p < 0 || p > GetLength(h) {
return nil
}
var i int = 0
n := h
for n.Next != nil {
i++
n = n.Next
if i == p {
return n
}
}
return nil
}
2. main.go代码如下:
package main
import "fmt"
import "list/singlechain"
func main() {
//初始化一个头结点
var h singlechain.Node
//往链表插入10个元素
for i := 1; i <= 10; i++ {
var d singlechain.Node
d.Data = i
singlechain.Insert(&h, &d, i)
fmt.Println(singlechain.GetLoc(&h, i))
}
fmt.Println(singlechain.GetLength(&h))
fmt.Println(singlechain.GetFirst(&h))
fmt.Println(singlechain.GetLast(&h))
fmt.Println(singlechain.GetLoc(&h, 6))
}
希望本文所述对大家的Go语言程序设计有所帮助。
标签:Go语言,单链表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vscode ssh安装librosa处理音频的解决方法
2022-04-25 04:33:54
![](https://img.aspxhome.com/file/2023/7/127237_0s.png)
Go语言学习技巧之如何合理使用Pool
2024-02-05 08:04:47
python tkinter中的锚点(anchor)问题及处理
2021-05-06 23:53:16
![](https://img.aspxhome.com/file/2023/0/108170_0s.jpg)
pandas 读取各种格式文件的方法
2023-12-14 22:08:59
Windows10下安装配置 perl 环境的详细教程
2022-11-17 01:46:32
![](https://img.aspxhome.com/file/2023/0/65830_0s.jpg)
python解析xml简单示例
2022-10-06 13:37:49
python学生管理系统学习笔记
2022-06-25 13:55:05
基于Python编写一个中秋节嫦娥投食小游戏
2022-12-27 10:46:34
![](https://img.aspxhome.com/file/2023/7/134457_0s.gif)
一个月入门Python爬虫学习,轻松爬取大规模数据
2022-03-10 11:04:26
![](https://img.aspxhome.com/file/2023/8/101148_0s.jpg)
django的model操作汇整详解
2022-05-16 03:59:46
![](https://img.aspxhome.com/file/2023/8/120878_0s.png)
python发送邮件实例分享
2021-07-17 23:14:23
![](https://img.aspxhome.com/file/2023/3/110433_0s.jpg)
python多进程下的生产者和消费者模型
2022-05-30 02:37:07
![](https://img.aspxhome.com/file/2023/7/65847_0s.png)
Python Web App开发Dockerfiles编写示例
2023-02-01 12:47:19
Python对象体系深入分析
2022-10-25 11:29:33
![](https://img.aspxhome.com/file/2023/3/81453_0s.png)
使用Python设置tmpfs来加速项目的教程
2023-12-24 09:38:34
javascript 函数式编程
2024-04-19 10:29:38
超好用:免费的图床
2023-03-31 09:55:42
![](https://img.aspxhome.com/file/2023/5/112055_0s.png)
详解Python中键盘鼠标的相关操作
2021-04-17 23:39:02
![](https://img.aspxhome.com/file/2023/2/88032_0s.png)
Python轻松管理与操作文件的技巧分享
2021-11-19 14:42:12
Django 路由层URLconf的实现
2021-01-18 17:05:13