大佬教程收集整理的这篇文章主要介绍了为什么元素和环结构为golang列表/环?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:我的意思是询问api而不是在实现中使用Element / Ring.实现仍然可以使用非导出类型,但是api给出并接受接口{},那么为什么要让用户进出Element / Ring?
func (l *List) BACk() interface{} { if l.len == 0 { return nil } return l.root.prev.Value }
列表仍然在内部使用Element,但它只是元素(未导出),因为它不会返回它,而只返回值.
由于它是链接列表,因此您希望能够将项目链接在一起并从一个项目导航到下一个项目或上一个项目.这需要一个结构,该结构保存指向下一个和前一个项目的指针,并允许您导航到这些项目(使用Next()和Prev()函数). Element结构用于此目的,它包含指向下一个/上一个项目的指针以及实际值.
这是结构的定义方式,它们也有各种成员函数
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 } 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{} }
容器/环没有你所暗示的“额外”结构.只有Ring结构将一个项链接到下一个/上一个项并且还保存该值. Ring没有开始/结束,因此不需要在整个环上运行结构或跟踪开始.
type Ring struct { next,prev *Ring Value interface{} // for use by client; untouched by this library }
以上是大佬教程为你收集整理的为什么元素和环结构为golang列表/环?全部内容,希望文章能够帮你解决为什么元素和环结构为golang列表/环?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。