list is a two-way link list
This structure has all the functions of a linked list.
type Element type Element struct { Value interface{} //The value stored in the element} func (e *Element) Next() *Element //Return the next element of the element, and if there is no next element, return nilfunc (e *Element) Prev() *Element//Returns the previous element of the element, and returns nil if there is no previous element.type List func New() *List //Return an initialized listfunc (l *List) Back() *Element //Get the last element of list lfunc (l *List) Front() *Element //Get the first element of list lfunc (l *List) Init() *List //list l Initialize or clear list lfunc (l *List) InsertAfter(v interface{}, mark *Element) *Element //Insert an element with a value of v after the element mark in list l and return the element. If the mark is not an element in list, the list will not change.func (l *List) InsertBefore(v interface{}, mark *Element) *Element//Insert an element with a value of v before the element mark in list l and return the element. If the mark is not an element in list, the list will not change.func (l *List) Len() int //Get the length of list lfunc (l *List) MoveAfter(e, mark *Element) //After moving element e to element mark, if element e or mark does not belong to list l, or e==mark, list l will not change.func (l *List) MoveBefore(e, mark *Element)//Before moving element e to element mark, if element e or mark does not belong to list l, or e==mark, list l will not change.func (l *List) MoveToBack(e *Element)//Move element e to the end of list l. If e does not belong to list l, then list will not change.func (l *List) MoveToFront(e *Element)//Move element e to the header of list l. If e does not belong to list l, then list will not change.func (l *List) PushBack(v interface{}) *Element//Insert an element with a value of v at the end of list l and return the element.func (l *List) PushBackList(other *List)//Insert another list at the end of list l, where l and other can be equal.func (l *List) PushFront(v interface{}) *Element//Insert an element with a value of v in the first part of list l and return the element.func (l *List) PushFrontList(other *List)//Insert another list in the header of list l, where l and other can be equal.func (l *List) Remove(e *Element) interface{}//If element e belongs to list l, delete it from list and return the value of element e.
Give examples to illustrate its usage
package main import ( "container/list" "fmt" ) func main() { l := () //Create a new list for i := 0; i < 5; i++ { (i) } for e := (); e != nil; e = () { () //Output list value, 01234 } ("") (().Value) //Output the value of the header element, 0 (().Value) //Output the value of the tail element, 4 (6, ()) //Insert an element with a value of 10 after the header element for e := (); e != nil; e = () { () //Output list value, 061234 } ("") (().Next(), ()) //The two elements in the first part are interchangeable for e := (); e != nil; e = () { () //Output list value, 601234 } ("") (()) //Move the tail element to the first for e := (); e != nil; e = () { () //Output list value, 460123 } ("") l2 := () (l) //Place the element in l at the end of l2 for e := (); e != nil; e = () { () //Output the value of l2, 460123 } ("") <span style="color:#FF0000;">() //Clear l</span> (()) //0 for e := (); e != nil; e = () { () //Output list value, no content } }
Supplement: Go standard library study notes - two-way linked list (container/list)
Overview
The container/list package implements the basic two-way linked list function, including the insertion, deletion, and movement of elements.
Linked List Elements
The elements in the linked list are defined as follows:
type Element struct { Value interface{} } func (e *Element) Next() *Element func (e *Element) Prev() *Element
The value of the element is obtained through the Value attribute. In addition, Element has two methods, Next and Prev, to obtain the previous element and the next element of the current element respectively.
Member functions
initialization
An empty list can be initialized by calling the New function or the Init method, and Init can also reset a list. The function declaration is as follows:
func New() *List func (l *List) Init() *List
Traversal
For linked lists, traversal is the most commonly used operation, and there are three steps in traversal operation:
The first step is to get a traversal starting point; use Front or Back to get the head and tail of a linked list, and its function declaration is as follows:
func (l *List) Front() *List func (l *List) Back() *List
The second step is to move from the current element to the next element; use the Prev and Next methods on the Element to move one element forward or backward.
The third step is to traverse the end condition; the traverse the end condition requires human judgment, and generally compare whether the current element is the end element.
insert
There are two insert methods in container/list, InsertAfter and InsertBefore, which are used to insert elements before or after an element. The method declaration is as follows:
func (l *List) InsertAfter(v interface{}, mark *Element) *Element func (l *List) InsertBefore(v interface{}, mark *Element) *Element
Add to
PushBack and PushFront are used to add elements at the beginning and end of a linked list (in addition, PushBackList and PushFrontList are added at once). The method declaration is as follows:
func (l *List) PushBack(v interface{}) *Element func (l *List) PushFront(v interface{}) *Element
delete
You can delete the specified elements on the linked list through the Remove method, and the method declaration is as follows:
func (l *List) Remove(e *Element) interface{}
Examples of usage
In fact, by integrating the previous content, you can realize the function of simply traversing the linked list. Here is a simple traversal implementation
package main import ( "fmt" "container/list" ) func main() { link := () for i := 0; i <= 10; i++ { (i) }// for p := (); p != (); p = () { ("Number", ) } }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.