List,go standard library doubly linked list structure
Defined:
A: = list. New ()
Usage examples:
Package Mainimport ("container/list" "FMT") Func main () {L:=list. New () L.pushback (1)//Insert the last position of a linked listL.pushback (2) L.pushback (3) L.pushfront (4)//Insert the first position of a linked listI:=1 forE: = L.front (); E! = nil; E =E.next () {fmt. Printf ("element%d:%d\n", I, E.value)}}
Output Result:
Element 1:4 element 2:1 element 3:2 element 4:3
When an element in a list is a struct, there is a slightly different way to get the elements for each item, as shown in the following example:
Package Mainimport ("container/list" "FMT") Type Userstruct{IDintnamestring}func Main () {L:=list. New () Item1:= User{id:101, Name:"name1"} item2:= User{id:102, Name:"name2"} l.pushback (item1) l.pushback (item2) I:=1 forE: = L.front (); E! = nil; E =E.next () {fmt. Printf ("element%d:id:%d, name:%s\n", I, E.value. (User). ID, E.value. (User). Name)//first escape after usei++ }}
The output is:
Element 1:id:101, name:name1 element 2:id:102, name:name2
Go language starting from zero (ii) basic usage of--list structure