Golang:List
List的接口
func New() *List //创建List
func (l *List) Back() *Element //返回List的上一个元素
func (l *List) Front() *Element //返回List下一个元素
func (l *List) Init() *List //初始化List
func (l *List) InsertAfter(v interface{}, mark *Element) *Element //在指定节点后插入,成功返回插入节点的指针,失败返回nil, 时间复杂度O(1)
func (l *List) InsertBefore(v interface{}, mark *Element) *Element //在指定节点之前插入, 成功返回插入节点的指针,失败返回nil, 时间复杂度O(1)
func (l *List) Len() int //返回链表长度,时间复杂度O(1)
func (l *List) MoveAfter(e, mark *Element) //移动节点e到mark节点之后,时间复杂度O(1), 处理方式:先删除然后再插入
func (l *List) MoveBefore(e, mark *Element) //移动节点e到mark节点之前,时间复杂度O(1), 处理方式:先删除然后再插入
func (l *List) MoveToBack(e *Element) //移动节点e到链表的尾部
func (l *List) MoveToFront(e *Element) //移动节点e到链表的头部
func (l *List) PushBack(v interface{}) *Element //在链表尾部追加值为v的新节点
func (l *List) PushBackList(other *List) //把链表other所有节点追加到当前链表的尾部
func (l *List) PushFront(v interface{}) *Element //在链表的头部插入新节点
func (l *List) PushFrontList(other *List) //把链表other所有节点追加到当前链表头部
func (l *List) Remove(e *Element) interface{} //删除指定节点
从这些接口我们可以看到Go的list应该是一个双向链表,不然InsertBefore这种操作应该不会放出来。
然后我们再从源码看看List的结构
// Element is an element of a linked list.
type Element struct {
// Next and previous pointers in the doubly-linked list of elements.
// To simplify the implementation, internally a list l is implemented
// as a ring, such that &l.root is both the next element of the last
// list element (l.Back()) and the previous element of the first list
// element (l.Front()).
next, prev *Element // The list to which this element belongs.
list *List // The value stored with this element.
Value interface{}
}
// List represents a doubly linked list.
// The zero value for List is an empty list ready to use.
type List struct {
root Element // sentinel list element, only &root, root.prev, and root.next are used
len int // current list length excluding (this) sentinel element
}
从这里证实了上面的猜想,这是一个双向链表
List的使用
package main import (
"container/list"
"fmt"
) func main() { list0 := list.New()
e4 := list0.PushBack()
e1 := list0.PushFront()
list0.InsertBefore(, e4)
list0.InsertAfter(, e1) for e := list0.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println("\r") front := list0.Front()
back := list0.Back() fmt.Println("front is:", front.Value)
fmt.Println("back is:", back.Value)
fmt.Println("length is", list0.Len()) list0.InsertBefore(, front)
list0.InsertAfter(, back) list0.PushFront(-)
list0.PushBack()
for e := list0.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println("\r")
list0.Remove(list0.Front())
for e := list0.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
} fmt.Println("\r")
list1 := list.New()
list1.PushBack()
list1.PushBack()
list0.PushBackList(list1)
for e := list0.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println("\r")
}
10-14行往list写入1,2,3,4
16行遍历打印list
21-16行打印front 和back节点
42-48行把另一个list追加到list的back
运行结果是:
front is:
back is:
length is
-
Golang:List的更多相关文章
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- 说说Golang的使用心得
13年上半年接触了Golang,对Golang十分喜爱.现在是2015年,离春节还有几天,从开始学习到现在的一年半时间里,前前后后也用Golang写了些代码,其中包括业余时间的,也有产品项目中的.一直 ...
- TODO:Golang指针使用注意事项
TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- golang struct扩展函数参数命名警告
今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...
- golang语言构造函数
1.构造函数定义 构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数 ,可根据其参数个 ...
- TODO:Golang语言TCP/UDP协议重用地址端口
TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...
- golang的安装
整理了一下,网上关于golang的安装有三种方式(注明一下,我的环境为CentOS-6.x, 64bit) 方式一:yum安装(最简单) rpm -Uvh http://dl.fedoraprojec ...
- golang枚举类型 - iota用法拾遗
在c#.java等高级语言中,经常会用到枚举类型来表示状态等.在golang中并没有枚举类型,如何实现枚举呢?首先从枚举的概念入手. 1.枚举类型定义 从百度百科查询解释如下:http://baike ...
- golang 使用 iota
iota是golang语言的常量计数器,只能在常量的表达式中使用. iota在const关键字出现时将被重置为0(const内部的第一行之前),const中每新增一行常量声明将使iota计数一次(io ...
随机推荐
- oracle填坑之PLSQL中文显示为问号
刚入坑oracle就遇到个坑. 坑描述: 系统:Windows7 oracle:同时安装,11g和12c(安装顺序,先装的12c然后装的11g) 坑:开始安装的12c用SQL Developer使用本 ...
- 2018-2019-2 20165205 《网络对抗技术》 Exp6 信息收集与漏洞扫描
2018-2019-2 20165205 <网络对抗技术> Exp6 信息收集与漏洞扫描 实验目标 掌握信息收集的最基本技能与常用工具的方式 实验内容 各种搜索技巧的应用 DNS IP注册 ...
- 设计一函数,求整数区间[a,b]和[c,d]的交集
问题: 设计一函数,求整数区间[a,b]和[c,d]的交集.(c/c++.Java.Javascript.C#.Python) 1.Python: def calcMixed(a,b,c,d): r ...
- eclipse启动tomcat正常,但是debug启动报错FATAL ERROR in native method:JDWP No transports initialized,jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197) ERROR: transport error 202: connect failed:Connection timed out
FATAL ERROR in native method:JDWP No transports initialized,jvmtiError=AGENT_ERROR_TRANSPORT_INIT(19 ...
- java高并发实战(三)——Java内存模型和线程安全
转自:https://blog.csdn.net/gududedabai/article/details/80816488
- python_13 面向对象
面向对象 类:把一类事物的相同特征和动作整合到一起就是类,类是一个抽象的概念 对象:就是基于类出而创建的一个具体的事物或实例(具体存在),其特征和动作整合到一起 面向对象设计:将一类具体事物的数据和动 ...
- JAVA_04
* a:局部代码块 * 在方法中出现:限定变量生命周期,及早释放,提高内存利用率 * b:构造代码块 (初始化块) * 在类中方法外出现:多个构造方法方法中相同的代码存放到一起,每次调用构造都执行,并 ...
- loadrunner飞机订票系统从登陆订票退票登出的脚本实现代码调试通过
在LR自带的飞机订票系统中,完整模拟一个用户从登陆->订票->退票->登出这样一个业务流程,分解每个事务为一个Action: 进入首页->登陆->进入订票页面->选 ...
- Ubuntu上部署tomcat后无法访问8080端口问题
在tomacat的bin目录下,修改startup.sh文件,添加信息: #set java environment export JAVA_HOME=/usr/java/jdk1.8.0_111 e ...
- Arthas进阶学习(常用命令)
Step1 下载demo-arthas-spring-boot.jar,再用java -jar命令启动: wget https://github.com/hengyunabc/katacoda-sce ...