链表

链表(Linked list),是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。

每个节点包含下一个节点的地址,这样把所有的节点串起来了,通常把 链表中的第一个节点叫做链表头

单链表

package main

import (
"fmt"
) type test struct {
name string
age uint8
intro string
next *test
} func printList(str *test) {
for str != nil {
fmt.Println(*str)
str = str.next
}
} func main() {
var test1 test
test1.name = "zhangsan"
test1.age = 123
test1.intro = "dasdasd" var test2 test
test2.name = "lisi"
test2.age = 33
test2.intro = "dadasdasdsdasd" test1.next = &test2 printList(&test1)
}

链表的头部插入

package main

import (
"fmt"
"math/rand"
) type test struct {
name string
age int
intro string
next *test
} func printlist(list *test) {
for list != nil {
fmt.Println(*list)
list = list.next
}
} func insertTop(list *test) {
for n := 0; n < 10; n++ {
var student *test = &test{
name: fmt.Sprintf("stu_%d", n),
age: rand.Intn(100),
intro: fmt.Sprintf("stu_dasdasdas_%d", n),
}
student.next = list //将student的next插入到list之前,即stu.next=list
list = student //将stu赋值为list // list.next = student.next
}
printlist(list)
} func main() { var stu *test = &test{
name: "lisi",
age: rand.Intn(100),
intro: "aaaaaaaaaaaaa",
} insertTop(stu) }

package main

import (
"fmt"
"math/rand"
) type test struct {
name string
age int
intro string
next *test
} func printlist(list *test) {
for list != nil {
fmt.Println(*list)
list = list.next
}
} func insertTop(list **test) { //指针的指针 for n := 0; n <= 10; n++ {
var student *test = &test{
name: fmt.Sprintf("stu_%d", n),
age: rand.Intn(100),
intro: fmt.Sprintf("stu__%d", n),
} student.next = *list
*list = student
}
} func main() {
var stu *test = &test{
name: "lisi",
age: rand.Intn(100),
intro: "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
} insertTop(&stu)
printlist(stu)
}

尾部插入

package main

import (
"fmt"
"math/rand"
) type test struct {
name string
age int
intro string
next *test
} func printlist(list *test) {
for list != nil {
fmt.Println(*list)
list = list.next
}
} func insertTail(list *test) { var test1 = list
for n := 0; n <= 10; n++ {
student := test{
name: fmt.Sprintf("stu_%d", n),
age: rand.Intn(100),
intro: fmt.Sprintf("stustustustustustustu%d", n),
} test1.next = &student
test1 = &student
}
} func main() { var stu = test{
name: "zhangsan",
age: 16,
intro: "zhangsanlisi",
} insertTail(&stu)
fmt.Println(&stu) }

golang基础数据结构链表的更多相关文章

  1. golang基础数据结构

    一.概述: 这里主要讨论四种类型---数组.slice.map和结构体 数组和结构体是聚合类型:它们的值都是由很多个元素或者成员字段的值组成.数组是有同构元素组成--每个数组的元素的类型相同:结构体为 ...

  2. 数据结构和算法(Golang实现)(12)常见数据结构-链表

    链表 讲数据结构就离不开讲链表.因为数据结构是用来组织数据的,如何将一个数据关联到另外一个数据呢?链表可以将数据和数据之间关联起来,从一个数据指向另外一个数据. 一.链表 定义: 链表由一个个数据节点 ...

  3. C语言 - 基础数据结构和算法 - 企业链表

    听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...

  4. C语言 - 基础数据结构和算法 - 单向链表

    听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...

  5. Redis 基础数据结构之二 list(列表)

    Redis 有 5 种基础数据结构,分别为:string (字符串).list (列表).set (集合).hash (哈希) 和 zset (有序集合). 今天来说一下list(列表)这种数据结构, ...

  6. golang基础知识之encoding/json package

    golang基础知识之json 简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.可以去json.org 查看json标准的清晰定义.json pack ...

  7. GoLang基础数据类型-切片(slice)详解

    GoLang基础数据类型-切片(slice)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 数组的长度在定义之后无法再次修改:数组是值类型,每次传递都将产生一份副本.显然这种数 ...

  8. GoLang基础数据类型--->数组(array)详解

    GoLang基础数据类型--->数组(array)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Golang数组简介 数组是Go语言编程中最常用的数据结构之一.顾名 ...

  9. Python 数据结构 链表

    什么是时间复杂度 时间频度:一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才知道.但是我们不可能也没有必要对每一个算法都进行上机测试,只需要知道那个算法花费的时间多,那个算法花费得 ...

随机推荐

  1. 七牛云域名DV SSL证书申请流程以及CDN融合加速配置

    从2017年起,苹果ios以及微信小程序都陆续要求请求连接request地址是使用HTTPS协议的.所以在项目开发阶段就要考虑解决https的问题,同时这也是为项目实际安全所考虑.最近我也是在折腾项目 ...

  2. Eclipse Creating a New Runnable JAR File 清理工作空间下的配置文件

    D:\workspacegit\.metadata\.plugins\org.eclipse.debug.core\.launches 相关文件存在这里.

  3. linux_查看磁盘与目录容量

    一.查看磁盘容量命令df(report file system disk space usage) 终端运行 $ df 输出结果 我的物理主机上的 /dev/sda5 是对应着主机硬盘的分区,字母 a ...

  4. React 模板

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  5. html 類 class

    為html元素設置類,相同的類使用相同的樣式.一般元素都能使用,可以在<div>和<span>中使用,同一個class可以被個<div>或<span>使 ...

  6. Minimum Cost POJ - 2516(模板题。。没啥好说的。。)

    题意: 从发货地到商家 送货 求送货花费的最小费用... 有m个发货地,,,n个商家,,每个商家所需要的物品和物品的个数都不一样,,,每个发货地有的物品和物品的个数也不一样,,, 从不同的发货地到不同 ...

  7. QAU 17校赛 J题 剪丝带(完全背包变形)

    题意: 剪一段丝带,对于剪完后的每一段丝带长度必须是a,b,c 输入丝带的长度  n 和  a  b  c 输出一个整数,代表最多能剪成多少段 样例输入 5 5 3 2 7 5 5 2 样例输出 2 ...

  8. LeetCode好题汇总

    最近开始刷LeetCode,准备按照专题来进行.所有的解题方案我都会放在GitHub上面,对于有价值的题目,我会重新在这里做记录,并且将解题方案贴出来,便于自己之后复习. Array 1. easy ...

  9. P1198 最大数 线段树水题

    这道题模拟一下可以过,但是我们发现线段树也可以安全水过...... 写的线段树只需要滋磁单点修改,区间求max即可 我一开始犯了一个很SB的错误:每次插入修改了t,然后疯狂爆0到怀疑人生... 而且我 ...

  10. Spring的后置处理器BeanFactoryPostProcessor

    新建一个JavaBean UserBeanFactoryPostProcessor 实现了BeanFactoryPostProcessor接口 Spring配置文件如下: 编写测试用例 从结果可以看出 ...