Golang标准库——container/list
list包实现了双向链表。
</blockquote> <h3>type Element</h3> <pre><code class="lang-go hljs">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{} } </code></code></pre> <blockquote>Element类型代表是双向链表的一个元素。
</blockquote> <h4>func (*Element) Next</h4> <pre><code class="lang-go hljs">func (e *Element) Next() *Element </code></code></pre> <blockquote>Next返回链表的后一个元素或者nil。
</blockquote> <h4>func (*Element) Prev</h4> <pre><code class="lang-go hljs">func (e *Element) Prev() *Element </code></code></pre> <blockquote>Prev返回链表的前一个元素或者nil。
</blockquote> <h3>type List</h3> <pre><code class="lang-go hljs">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 } </code></code></pre> <blockquote>代表一个双向链表。List零值为一个空的、可用的链表。
</blockquote> <h4>func New</h4> <pre><code class="lang-go hljs">func New() *List </code></code></pre> <blockquote>New创建一个链表。
</blockquote> <h4>func (*List) Init</h4> <pre><code class="lang-go hljs">func (l *List) Init() *List </code></code></pre> <blockquote>Init清空链表。
</blockquote> <h4>func (*List) Len</h4> <pre><code class="lang-go hljs">func (l *List) Len() int </code></code></pre> <blockquote>Len返回链表中元素的个数,复杂度O(1)。
</blockquote> <h4>func (*List) Front</h4> <pre><code class="lang-go hljs">func (l *List) Front() *Element </code></code></pre> <blockquote>Front返回链表第一个元素或nil。
</blockquote> <h4>func (*List) Back</h4> <pre><code class="lang-go hljs">func (l *List) Back() *Element </code></code></pre> <blockquote>Back返回链表最后一个元素或nil。
</blockquote> <h4>func (*List) PushFront</h4> <pre><code class="lang-go hljs">func (l *List) PushFront(v interface{}) *Element </code></code></pre> <blockquote>PushBack将一个值为v的新元素插入链表的第一个位置,返回生成的新元素。
</blockquote> <h4>func (*List) PushFrontList</h4> <pre><code class="lang-go hljs">func (l *List) PushFrontList(other *List) </code></code></pre> <blockquote>PushFrontList创建链表other的拷贝,并将拷贝的最后一个位置连接到链表l的第一个位置。
</blockquote> <h4>func (*List) PushBack</h4> <pre><code class="lang-go hljs">func (l *List) PushBack(v interface{}) *Element </code></code></pre> <blockquote>PushBack将一个值为v的新元素插入链表的最后一个位置,返回生成的新元素。
</blockquote> <h4>func (*List) PushBackList</h4> <pre><code class="lang-go hljs">func (l *List) PushBackList(other *List) </code></code></pre> <blockquote>PushBack创建链表other的拷贝,并将链表l的最后一个位置连接到拷贝的第一个位置。
</blockquote> <h4>func (*List) InsertBefore</h4> <pre><code class="lang-go hljs">func (l *List) InsertBefore(v interface{}, mark *Element) *Element </code></code></pre> <blockquote>InsertBefore将一个值为v的新元素插入到mark前面,并返回生成的新元素。如果mark不是l的元素,l不会被修改。
</blockquote> <h4>func (*List) InsertAfter</h4> <pre><code class="lang-go hljs">func (l *List) InsertAfter(v interface{}, mark *Element) *Element </code></code></pre> <blockquote>InsertAfter将一个值为v的新元素插入到mark后面,并返回新生成的元素。如果mark不是l的元素,l不会被修改。
</blockquote> <h4>func (*List) MoveToFront</h4> <pre><code class="lang-go hljs">func (l *List) MoveToFront(e *Element) </code></code></pre> <blockquote>MoveToFront将元素e移动到链表的第一个位置,如果e不是l的元素,l不会被修改。
</blockquote> <h4>func (*List) MoveToBack</h4> <pre><code class="lang-go hljs">func (l *List) MoveToBack(e *Element) </code></code></pre> <blockquote>MoveToBack将元素e移动到链表的最后一个位置,如果e不是l的元素,l不会被修改。
</blockquote> <h4>func (*List) MoveBefore</h4> <pre><code class="lang-go hljs">func (l *List) MoveBefore(e, mark *Element) </code></code></pre> <blockquote>MoveBefore将元素e移动到mark的前面。如果e或mark不是l的元素,或者e==mark,l不会被修改。
</blockquote> <h4>func (*List) MoveAfter</h4> <pre><code class="lang-go hljs">func (l *List) MoveAfter(e, mark *Element) </code></code></pre> <blockquote>MoveAfter将元素e移动到mark的后面。如果e或mark不是l的元素,或者e==mark,l不会被修改。
</blockquote> <h4>func (*List) Remove</h4> <pre><code class="lang-go hljs">func (l *List) Remove(e *Element) interface{} </code></code></pre> <blockquote>Remove删除链表中的元素e,并返回e.Value。
</blockquote> <pre><code class="lang-go hljs">func main() { l := list.New() for i := 0; i < 5; i { l.PushBack(i) } for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value) //01234 } fmt.Println("") fmt.Println(l.Front().Value) //0 fmt.Println(l.Back().Value) //4 l.InsertAfter(6, l.Front()) //首部元素之后插入一个值为6的元素 for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value) //061234 } fmt.Println("") l.MoveBefore(l.Front().Next(), l.Front()) //首部两个元素位置互换 for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value) //601234 } fmt.Println("") l.MoveToFront(l.Back()) //将尾部元素移动到首部 for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value) //,460123 } fmt.Println("") l2 := list.New() for i := 0; i < 5; i { l2.PushBack(i) } l2.PushBackList(l) //将l中元素放在l2的末尾 for e := l2.Front(); e != nil; e = e.Next() { fmt.Print(e.Value) //01234460123 } fmt.Println("") l.Init() //清空l fmt.Print(l.Len()) //0 for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value) // } } </code></code></pre> 到此这篇关于“ Golang标准库——container/list”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!您可能感兴趣的文章:
更改MySQL数据库名实例代码
linux下PostgreSQL的安装与使用
php实现简单用户登录功能程序代码
css中position相对定位和绝对定位(relative,absolute)详解
网页标题随机显示名言js代码
Golang标准库——container/list
PHP无限级分类菜单实例程序
专家教你如何有效的学习Drupal - Drupal问答
PHP中截取中文乱码解决办法
用于管理iptables的shell脚本一例