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的更多相关文章

  1. Golang, 以17个简短代码片段,切底弄懂 channel 基础

    (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...

  2. 说说Golang的使用心得

    13年上半年接触了Golang,对Golang十分喜爱.现在是2015年,离春节还有几天,从开始学习到现在的一年半时间里,前前后后也用Golang写了些代码,其中包括业余时间的,也有产品项目中的.一直 ...

  3. TODO:Golang指针使用注意事项

    TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...

  4. Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩

    目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...

  5. golang struct扩展函数参数命名警告

    今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...

  6. golang语言构造函数

    1.构造函数定义 构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数 ,可根据其参数个 ...

  7. TODO:Golang语言TCP/UDP协议重用地址端口

    TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...

  8. golang的安装

    整理了一下,网上关于golang的安装有三种方式(注明一下,我的环境为CentOS-6.x, 64bit) 方式一:yum安装(最简单) rpm -Uvh http://dl.fedoraprojec ...

  9. golang枚举类型 - iota用法拾遗

    在c#.java等高级语言中,经常会用到枚举类型来表示状态等.在golang中并没有枚举类型,如何实现枚举呢?首先从枚举的概念入手. 1.枚举类型定义 从百度百科查询解释如下:http://baike ...

  10. golang 使用 iota

    iota是golang语言的常量计数器,只能在常量的表达式中使用. iota在const关键字出现时将被重置为0(const内部的第一行之前),const中每新增一行常量声明将使iota计数一次(io ...

随机推荐

  1. nginx配置多个域名

    1.原来的80端口改掉,下面配置: server { listen 80; server_name *.pobohn.com; location / { proxy_pass http://local ...

  2. python源码探秘:用户函数的执行过程

    脚本函数编译后如何执行?脚本编译后是pyc码,pycodeobject对象的串行化.import时是对pyc文件反系列化.函数编译后会生成函数对象,函数对象的TP_call对应的是function_c ...

  3. nodeJs 使用 express-http-proxy 转发请求

    开发过程中经常需要用到 nodeJs做转发层 使用express配合 express-http-proxy 可以轻松的完成转发 使用过程: 安装 express-http-proxy npm inst ...

  4. CSS设计中的错误大整理!

    如果有人发明时间机器,那应该将这些错误纠正,不然可把前端程序猿们给还惨了.大家一起看看都有哪些CSS规则应该完善. (CSS 代码) white-space: nowrap 应该 white-spac ...

  5. Centos7 操作系统 mysql5.7 配置远程登陆操作

    Centos7 操作系统: mysql5.7 配置远程登陆操作: 首先登陆服务器,进入数据库: mysql -u root -p show databases; use mysql; show tab ...

  6. AWS 实战

    AWS (Amazon Web Service) 要使用亚马逊的免费云服务 AWS,必须先注册账号,然后绑定信用卡. 创建 AMI(Amazon Machine Image) 选择 EC2 服务 EC ...

  7. cookies,sessionstorage,localstorage的区别?

    请描述一下 cookies,sessionStorage 和 localStorage 的区别? sessionStorage 和 localStorage 是HTML5 Web Storage AP ...

  8. AlphaGo的前世今生(三)AlphaGo Zero: AI Revolution and Beyond

    这是本专题的第三节,在这一节我们将以David Silver等人的Natrue论文Mastering the game of Go without human knowledge为基础讲讲AlphaG ...

  9. 浅谈Spring

    参考文章: https://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/ 参考书籍: <SPRING技术内幕:深入解析SPR ...

  10. windows系统如何安装运行filebeat

    下载安装包 下载地址:https://www.elastic.co/downloads/beats/filebeat 解压到指定目录,无需安装 打开解压后的目录,打开filebeat.yml进行配置. ...