Go Language (Container introduction)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Today, Mayuyu to introduce an important thing in the go language, called container. The specific source code can refer to the source of the Go language, as follows

Path :/usr/local/go/src/pkg/container

the path on GitHub is: Https://github.com/astaxie/gopkg/tree/master/container

The structure of the container is as follows

It can be seen that three parts are included: Heap,list and Ring. The following are respectively

1. Heap

Heap is a common data structure, in the source code, provides the interface, in actual use needs to be implemented.

Here is an example of the heap usage.

Code:

Package Mainimport ("FMT" "Container/heap")//heap provides an interface that requires itself to implement the following method of type heap []int//constructs a small top heap, the large top heap only needs to change the following symbol 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 () {//Create a heaph: = &heap{}    Heap. Init (h)//Insert element H into the heap. Push (5) H.push (2) H.push (1) h.push (8) H.push (4) H.push (6) H.push (2)//output heap elements, equivalent to an array, the original array fmt. Println (h)//Must be reheapify here, set up heaps of heap. Init (h)//small top heap corresponds to the position of the element in the array of FMT. Println (h)//Remove the subscript 5 element, subscript starting from 0 h. Remove (5)//output in the form of a heap for H.len () > 0 {fmt. Printf ("%d", heap. Pop (h))}fmt. Println ()}


2. List

The list type is a doubly linked list, with the following specific usage

Code:

Package Mainimport ("FMT" "Container/list") func main () {//Create a doubly linked list ls: = list. New ()//Insert 26 lowercase letters for I: = 97 in a doubly linked list; I < 123; i++ {ls. Pushfront (i)     //pushfront () represents the insertion from the head, the same pushback () represents the insertion from the tail}//traversal of 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 ()}


3. Ring

Ring is a ring linked list, the specific use can refer to the following code

Code:

Package Mainimport ("FMT" "container/ring"//closed loop packet introduced, see/usr/local/go/src/pkg/container/ring) func main () {//Create 10 elements of closed loop R : = Ring. New (10)//Assign a value for the element in the closed loop for I: = 1; I <= R.len (); i++ {r.value = IR = R.next ()}    //loop prints the element value R in the closed loop. Do (func (P interface{}) {println (P)    })//Gets the 5th element after the current element R5: = R.move (5) fmt. Println (R5) fmt. PRINTLN (r)//link the current element R with R5, which is equivalent to removing the element between R and R5 r1: = R.link (R5) fmt. Println (R1) fmt. Println (R)}


Related Article

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.