Container/list Package usage in Golang

Source: Internet
Author: User
This is an article created in 2015-06-23 02:03:13, where the information may have evolved or changed.

List is a doubly linked list. The structure has all the functions of a linked list.

Type Element

Type element struct {value

        interface{}   //values stored in element
}

Func (e *element) Next () *element//Returns the next element of the element, or nil if there is no next element
Func (e *element) Prev () *element//returns the previous element of the element, or nil if there is no previous element.

Type List
Func New () *list//returns an initialized List
Func (L *list) back () *element//Get the last element of List l
Func (L *list) Front () *element//Get the first element of List L
Func (L *list) init () *list//list L initialize or clear List l
Func (L *list) InsertAfter (v interface{}, Mark *element) *element//Inserts an element with a value of V after the element mark in list L, and returns the element if Mark is not an element in the list, The list does not change.
Func (L *list) insertbefore (v interface{}, Mark *element) *element//inserts an element with a value of V before the element mark in list L, and returns the element if Mark is not an element in the list. The list does not change.
Func (L *list) Len () int//Get the length of List l
Func (L *list) MoveAfter (E, Mark *element)//moves element e to element mark, if element e or mark does not belong to list L, or E==mark, then list L does 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, then list L does not change.
Func (L *list) movetoback (e *element)//Moves element E to the end of List L, and if E does not belong to list L, the list does not change.
Func (L *list) Movetofront (e *element)//Moves element E to the header of List L, and if E does not belong to list L, the list does not change.
Func (L *list) pushback (v interface{}) *element//inserts an element with a value of V at the end of the List L and returns the element.
Func (L *list) pushbacklist (other *list)//Inserts another list at the tail of List L, where L and other can be equal.
Func (L *list) Pushfront (v interface{}) *element//inserts the element with the value V in the header of the List L and returns the element.
Func (L *list) pushfrontlist (other *list)//Inserts another list at the header of the list L, where L and other can be equal.
Func (L *list) remove (e *element) interface{}//if element e belongs to list L, removes it from list and returns the value of element E.

illustrate its usage.

Package main import ("Container/list" "FMT") func main () {l: = list. New ()//Create a list for I: = 0; I < 5; i++ {l.pushback (i)} for E: = L.front (); E! = nil; E = E.next () {fmt. Print (e.value)//Output list value, 01234} fmt. Println ("") fmt. Println (L.front (). Value)//outputs the values of the header element, 0 FMT. Println (L.back (). Value)//output trailing element, 4 L.insertafter (6, L.front ())//The first element after inserting a value of 10 element for E: = L.front (); E! = nil; E = E.next () {fmt. Print (e.value)//Output list value, 061234} fmt. Println ("") L.movebefore (L.front (). Next (), L.front ())//first two element position interchange for E: = L.front (); E! = nil; E = E.next () {fmt. Print (e.value)//Output list value, 601234} fmt. Println ("") L.movetofront (L.back ())//moves the trailing element to the header for e: = L.front (); E! = nil; E = E.next () {fmt. Print (e.value)//Output list value, 460123} fmt. Println ("") L2: = list. New () L2. Pushbacklist (L)//The element in L is placed at the end of L2 for E: = L2. Front (); E! = nil; E = E.next () {fmt. Print (e.value)//Output L2 value, 460123} fmt. Println ("") <span style= "color: #FF0000;"       >l.init ()    Empty l</span> FMT. Print (L.len ())//0 for E: = L.front (); E! = nil; E = E.next () {fmt. Print (e.value)//Output list value, no content}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.