Go Language (container learning)

This article comes from: CSDN blog

Thanks Author: ACHelloWorld

See original: Go Language (container description)

container:容器 | container

As can be seen comprising three parts: heap, list and ring. The following were introduced

1. heap

   heap is the heap, is a common data structure, in which the source, provides the interface needs to be implemented in practical use.

   The following is an example on the use of heap.

   Code:

main Package 

Import ( 
    " FMT " 
    " Container / heap "     
) 

// heap provides the interface need to implement a method 
type Heap [] int 

// configuration of the top stack is small, a large pile top change it need only following symbols 
func (H * Heap) Less (I, J int ) BOOL {
     return (* H) [I] <(* H) [J] 
} 

FUNC (H * Heap) Swap (I, J int ) { 
    ( * H) [ I], (H *) [J] = (H *) [J], (* H) [I] 
} 

FUNC (H * Heap) len () int {
     return len (* H) 
} 

FUNC (H*Heap) Pop() interface{} {
    x := (*h)[h.Len() - 1]
    *h = (*h)[: h.Len() - 1]
    return x
}

func (h *Heap) Push(x interface{}) {
    *h = append(*h, x.(int))
}

func (h *Heap) Remove(idx int) interface{} {
    h.Swap(idx, h.Len() - 1)
    return h.Pop()
}

func main(){
    
    //创建一个heap
    h := &Heap{}
    
    heap.Init(h)
    //Insert elements into the heap 
    h.Push ( . 5 ) 
    h.Push ( 2 ) 
    h.Push ( . 1 ) 
    h.Push ( . 8 ) 
    h.Push ( . 4 ) 
    h.Push ( . 6 ) 
    h.Push ( 2 ) 

    // Output heap of elements, the equivalent of an array, the original array 
    fmt.Println (H) 

    // this must be reheapify, establish a good heap of 
    heap.Init (H) 

    // small pile top position corresponding element in the array of 
    fmt. println (H) 

    // remove the labeled element 5, and the subscript starts from 0 
    (h.Remove 5 ) 

    // output in the form of a stack 
    for h.Len ()> 0  {
        fmt.Printf ("%d ", heap.Pop(h))
    }
    fmt.Println()
}

 

2. list

   list is doubly linked list type, are used as follows

   Code:

main Package 

Import ( 
    " FMT " 
    " Container / List "     
) 

FUNC main () { 

    // create a doubly linked list 
    LS: = list.New () 

    // inserted into the doubly linked list 26 lowercase letters 
    for I: = 97 ; I < 123 ; I ++ { 
        ls.PushFront (I)      // PushFront () on behalf of the head portion is inserted, the same PushBack, () on behalf inserted from the rear 
    } 

    // iterate through all the letters in the doubly linked list ls 
    for IT: = ls.Front () ; IT = nil;! IT = it.Next () { 
        fmt.Printf ( " % C " , it.Value) 
    } 
    fmt.Println () 
}

ListAnd Element. The former achieved a doubly linked list (hereinafter referred to as the list), which represents the structure of the list elements.

ListAnd Elementall types of structures. Structure type has a characteristic, that is, they have zero value will be its particular structure, but no value any custom content, the equivalent of a shell


List of main methods:

MoveBefore: Given elements to the front of the other elements

MoveAfter: Given element moves behind another element

MoveToFront: The given element is moved to the forefront of the list

MoveToBack: The given element is moved to the rearmost end of the list

Front: Get the forefront of the list

Back: Get the list of the last end

InsertBeforeInsert a new element before the specified elements:

InsertAfter: Insert a new element after the specified element

PushFront: Used for inserting a new element at the head of the list.

PushBack: Used for inserting a new element at the head of the list.

 

3. ring

   ring is a circular linked list, you can refer to the specific use of the following code

   Code:

main Package 

Import ( 
    " FMT " 
    " Container / Ring "     // closed package is introduced, see / usr / local / Go / the src / PKG / Container / Ring 
) 

FUNC main () { 
    // create a loop elements 10 
    r: as ring.New = ( 10 ) 

    // for closed loop element assignments 
    for I: = . 1 ; I <= r.Len (); I ++ { 
        r.Value = I 
        R & lt = r.Next () 
    } 

    // loop to print loop the element value 
    r.Do ( 
        FUNC (P interface {}) { 
            the println (P) 
        }) 
    
    // after the current element of the obtained elements 5
    R5: r.Move = ( . 5 ) 
    fmt.Println (R5) 
    fmt.Println (r) 

    // Link the current element r and r5, the elements corresponding to the deleted between r and R5 
    R1: = r.Link (R5) 
    fmt.Println (R1) 
    fmt.Println (R & lt) 
}

 

Guess you like

Origin www.cnblogs.com/-wenli/p/12500002.html