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. Eclipse Tomcat部署web项目时出现There are no resources that can be added or removed from the server解决办法

    问题原因是:tomcat版本和java版本不匹配.

  2. VS连接数据库字符串

    在App.config配置文件中的<Configuration>节点中添加如下代码 <connectionStrings>    // SQL Server 数据库      ...

  3. iOS Simulator version 11 or later is currently not supported.

    iOS Simulator version 11 or later is currently not supported.You can open Xcode > Preferences > ...

  4. 自学web前端能不能找到一份前端的工作吗

    关于自学web前端能不能通过社招找到一份互联网公司web前端开发的工作,有无数的人问出这样的问题,答案没有标准的,只能从概率去考虑.有的人可以,有的人不可以,有的人自学就业的概率就是高,有的概率就是低 ...

  5. oracle数据库误删的表以及表中记录的恢复

    oracle数据库误删的表以及表中记录的恢复 一.表的恢复 对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有: --1.从flash bac ...

  6. GDI+_绘制QQ头像

    Public Sub I_touxiang(ByVal file As String, ByVal Graphics As Long, Width As Long, Height As Long, O ...

  7. MongoDB 集合(Collection)对应的物理文件

    dbpath下是清一色的collection-n-***与index-n-***开头的物理文件,如何知道某一个集合与其对应与其对应的物理文件? db.collection_name.stats() 返 ...

  8. 《DOM Scripting》学习笔记-——第八章 充实文档的内容

    本章内容 一.一个为文档创建“缩略词语表”的函数 二.一个为文档创建“文献来源链接”的函数 三.一个为文档创建“快速访问键清单”的函数 利用DOM动态的收集和创建一些有用的辅助信息,并把它们呈现在网页 ...

  9. python基础之语句字符串

    python的种类: jpython                 java写的python ironpython            c#写的python cpython             ...

  10. 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)

    承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行         在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...