Golang linked List
Brief introduction
The Golang itself implements a doubly linked list
Import
import "container/list"
Defined
List elements Type element func (e *element) Next () *element func (e *element) Prev () *element//linked list Type----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 func (L *list) insertbefore (v interface{}, Mark *element) *element Func (L *list) Len () int func (l *list) MoveAfter (E, Mark *element) func (L *list) MoveBefore (E, Mark *element) Func (L *list) movetoback (e *element) func (L *list) Movetofront (e *element) func (L *list) pushback (v interface{} ) *element func (L *list) pushbacklist (Other *list) func (L *list) Pushfront (v interface{}) *element func (L *lis T) pushfrontlist (other *list) func (L *list) Remove (e *element) interface{}//Element is defined as follows, the type of value is interface type, so we can Implements any type of linked list type Element struct {//The value stored with this Element. Value interface{}//contains filtered or unexported fields}
As an example,
package mainimport ( "container/list" "fmt")func main() { //创建一个list l := list.New() e4 := l.PushBack(4) e1 := l.PushFront(1) l.InsertBefore(3, e4) l.InsertAfter(2, e1) for e := l.Front(); e != nil; e = e.Next() { fmt.Println(e.Value) }}
Source Code Implementation
Source directory:/usr/local/go/src/container/list (my)
Copyright: The Go Authors. All rights reserved.//Use of this source code are governed by a bsd-style//license so can be found in the license file. Package list implements a doubly linked list.////to iterate over a list (where L was a *list)://for E: = L.front (); E! = nil; E = E.next () {////do something with e.value//}//package list//Element are an element of a linked List.type El ement struct {//Next and previous pointers in the doubly-linked list of elements. To simplify the implementation, internally a list L was implemented//as a ring, such that &l.root is both the N ext 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{}}//Next returns the next list element or Nil.func (e *element) next () *element {if p: = E.next; e.li St! = Nil && P! = &e.list.root {return P} return nil}//Prev returns the previous list element or Nil.func (e *element) Prev () *element {if p: = E.prev; E.list! = Nil && P! = &e.list.root {return P} return nil}//list represents a doubly linked list.//the zero value for List are an empty List ready to Use.type list struct {root Element/Sentinel list Element, only &root, Root.prev, and root.next is used Len int Current list length excluding (this) Sentinel element}//init initializes or clears list L.func (L *list) init () *list {L.root.next = &l.root L.root.prev = &l.root L.len = 0 return l}//New returns an initialized LIST.F UNC new () *list {return New (List). Init ()}//Len returns the number of elements of the list l.//the complexity is O (1). Func (L *list) len () int {return L.len }//Front Returns the first element of the list L or Nil.func (L *list) Front () *element {if L.len = = 0 { Return nil} return l.root.next}//back returns the last element of a list L or Nil.func (L *list) back () *element { If L.len = = 0 {return nil} return l.root.prev}//Lazyinit lazily Initializes a zero List Value.func (l *list) Lazyinit () {if L.root.next = = nil {l.init ()}}//Insert inserts E after at, increments L.len, and re Turns E.func (l *list) insert (E, at *element) *element {n: = at.next At.next = e-E.prev = at e.next = n N. Prev = e e.list = l l.len++ return e}//Insertvalue is a convenience wrapper for insert (&element{value:v}, a T). Func (L *list) insertvalue (v interface{}, at *element) *element {return L.insert (&element{value:v}, at)}//REM Ove removes E from its list, decrements L.len, and returns E.func (L *list) remove (e *element) *element {e.prev.next = E.next E.next.prev = E.prev E.next = nil//Avoid memory leaks E.prev = nil//Avoid memory leaks e.list = ni L l.len--return E}//remove removes E from L if E was an element of list l.//It returns the element value E.value.func (l *list) Remove (E * Element) interface{} {if e.list = = L {//if e.list = = L, l must has been initialized when E is inserted In L or L = Nil (E is a zero Element) and L.remove would crash L.remove (e)} return e.value}//Pushfron T inserts a new element E with value V at the front of List L and returns E.func (L *list) Pushfront (v interface{}) *eleme NT {l.lazyinit () return L.insertvalue (V, &l.root)}//pushback inserts a new element E with value V at the back Of list L and returns E.func (L *list) pushback (v interface{}) *element {l.lazyinit () return L.insertvalue (V, L.roo T.prev)}//InsertBefore inserts a new element E with value v immediately before Mark and returns e.//If Mark isn't an El Ement of L, the list is not Modified.func (l *list) insertbefore (v interface{}, Mark *element) *element {if mark.list ! = L {return nil }//See comment in List.remove about initialization of L return L.insertvalue (V, mark.prev)}//InsertAfter Inse RTS a new element E with value v immediately after Mark and returns e.//If mark was not a element of L, the list was not m Odified.func (L *list) InsertAfter (v interface{}, Mark *element) *element {if mark.list! = L {return nil} See comment in List.remove about initialization of L return L.insertvalue (V, Mark)}//Movetofront moves element E To the front of list l.//If E was not a element of L, the list is not Modified.func (l *list) Movetofront (e *element) { If e.list! = L | | L.root.next = = e {return}//See comment in List.remove about initialization of L L.insert (L.remove (E), &l.root)}//Movetoback moves element e to the back of list l.//If E was not a element of L, the list is not modified. Func (L *list) movetoback (e *element) {if e.list! = L | | l.root.prev = e {return}//See comment in Li St.Remove about initialization of L L.insert (L.remove (e), L.root.prev)}//MoveBefore moves element e to its new position B Efore mark.//If E or mark is a element of l, or E = = Mark, the list is not Modified.func (l *list) movebefore (E, Mar K *element) {if e.list! = L | | | e = Mark | | Mark.list! = l {return} l.insert (L.remove (e), Mark.prev)}// MoveAfter moves element e to it new position after mark.//If E or mark was not a element of l, or E = = Mark, the list I s not Modified.func (L *list) MoveAfter (E, Mark *element) {if e.list! = L | | e = = Mark | | Mark.list! = L {Retu RN} l.insert (L.remove (e), Mark)}//pushbacklist Inserts a copy of the other list at the back of the list l.//the lists L and other is the Same.func (L *list) pushbacklist (other *list) {l.lazyinit () for I, E: = other. Len (), other. Front (); i > 0; I, E = I-1, E.next () {L.insertvalue (E.value, L.root.prev)}}//pushfrontlist Inserts a copy of an and list at The FRont of list l.//the lists L and other May is the Same.func (L *list) pushfrontlist (other *list) {L.lazyinit () for I, E: = other. Len (), other. Back (); i > 0; I, E = I-1, E.prev () {L.insertvalue (E.value, &l.root)}}