go语言 list用法是什么


本文摘自php中文网,作者藏色散人,侵删。

go语言中list的用法:1、通过“l := list.New()”方式声明链表;2、使用“list.Remove(element)”方式删除元素;3、使用“list1.PushBackList(list2)”方式合并两个链表即可。

本文环境:Windows10系统、Go1.11.2版,本文适用于所有品牌的电脑。

推荐:《golang教程

golang list用法笔记

依赖

1

2

3

4

import (

        "container/list"

        "fmt"

)

遍历

go的list也是用双向循环链表实现的,在尾部追加用PushBack()

1

2

3

4

5

6

7

8

9

10

// 声明链表

l := list.New()

// 数据添加到尾部

l.PushBack(4)

l.PushBack(5)

l.PushBack(6)

// 遍历

for e := l.Front(); e != nil; e = e.Next() {

     fmt.Printf("%v\n", e.Value)

}

删除元素

删除使用list.Remove(element)

1

2

3

4

l := list.New()

l.PushBack(4)

six := l.PushBack(6)

l.Remove(six) // 删除6这个节点

合并两个链表(list1)
这里面使用list1.PushBackList(list2)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

l := list.New()

l.PushBack(1)

l.PushBack(2)

l.PushBack(3)

l2 := list.New()

l2.PushBack(4)

l2.PushBack(5)

l2.PushBack(6)

l2.PushBackList(l2)

fmt.Printf("merge after l================\n")

for e := l.Front(); e != nil; e = e.Next() {

        fmt.Printf("%d\n", e.Value.(int))

}

fmt.Printf("merge after l2================\n")

for e := l2.Front(); e != nil; e = e.Next() {

        fmt.Printf("%d\n", e.Value.(int))

}

以上就是go语言 list用法是什么的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Go语言有goto吗

关于go使用mysql测试

Go语言通过命令方式生成可执行文件

Go语言多维数组

Go语言的init函数详解

Go语言是脚本语言吗

yum怎么安装Go语言

Go语言 break 语句

Go语言的特色是什么

Go语言编译快吗

更多相关阅读请进入《Go语言》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...