golang中container/list包源码分析
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了golang中container/list包源码分析,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2268字,纯文字阅读大概需要4分钟。
内容图文
![golang中container/list包源码分析](/upload/InfoBanner/zyjiaocheng/1096/51fdde14a58444eeb84483f95fddbd15.jpg)
golang源码包中container/list实际上是一个双向链表
提供链表的一些基本操作,下面就结合定义和接口进行下说明
1. 定义
// 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{} }
Element是数据节点, 提供Next()/Prev()这类常规操作
// 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双向链表定义
2. 接口说明
func New() *List //创建一个双向链表 func (l *List) Back() *Element //返回双向链表上一个元素 func (l *List) Front() *Element //返回双向链表下一个元素 func (l *List) Init() *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{} //删除指定节点
3. 实例
// Create a new list and put some numbers in it. l := list.New() e4 := l.PushBack(4) e1 := l.PushFront(1) l.InsertBefore(3, e4) l.InsertAfter(2, e1) // Iterate through list and print its contents.for e := l.Front(); e != nil; e = e.Next() { fmt.Println(e.Value) } Output: 1234
原文:http://www.cnblogs.com/davygeek/p/5916366.html
内容总结
以上是互联网集市为您收集整理的golang中container/list包源码分析全部内容,希望文章能够帮你解决golang中container/list包源码分析所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。