List of Go implementation sort

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

The data structure of the linked list is compared with the linear array, the advantage is that it can be conveniently inserted and deleted.

This feature makes it very suitable for applications in the sort scenario, because the Golang current class library is not very perfect, in Java can be very simple to use the API provides support to complete the list or map of the order, when using go is not so lucky, you may need to implement.

The following example is a list of sorts that are implemented using LinkedList in the Go package.

    • There are several functional features:

1. Support for fixed lengths

2. Customizable rules for sorting

3. Combined LinkedList function

Package Codeforfunimport ("Container/list") type sortedlinkedlist struct {*list. List Limit int comparefunc func (old, New interface{}) Bool}func newsortedlinkedlist (Limit int, compare func (old, n EW interface{}) bool) *sortedlinkedlist {return &sortedlinkedlist{list. New (), Limit, Compare}}func (this sortedlinkedlist) findinsertplaceelement (Value interface{}) *list. element {for element: = this. Front (); Element! = Nil; element = element. Next () {tempvalue: = element. Value if This.comparefunc (Tempvalue, value) {return element}} return Nil}func (this Sort Edlinkedlist) Putontop (value interface{}) {if this. List.len () = = 0 {this. Pushfront (value) return} if this. List.len () < this. Limit && This.comparefunc (value, this. Back (). Value) {this. Pushback (value) return} if This.comparefunc (this. List.front (). Value, value) {this. Pushfront (value)} else if This.comparefunc(this.) List.back (). Value, Value) && This.comparefunc (value, this. Front (). Value) {element: = This.findinsertplaceelement (value) if element! = Nil {this. InsertBefore (value, Element)}} if this. Len () > this. Limit {this. Remove (this. Back ())}}
    • How to use:
Package Mainimport (    "FMT"    "Codeforfun") type WordCount struct {    Word  string    Count Int}func CompareValue (old, new Interface {}) bool {    if new. ( WordCount). Count > Old. (WordCount). Count {        return True    }    return False}func main () {    wordcounts: = []wordcount{        wordcount{"Kate", wordcount{},        "Herry", "wordcount{",        "James", "Bayi}"    var asortedlinkedlist = Codeforfun. Newsortedlinkedlist (CompareValue)    for _, WordCount: = Range wordcounts {        asortedlinkedlist.putontop ( WordCount)    } for    element: = ASortedLinkedList.List.Front (); Element! = Nil; element = element. Next () {        fmt. Println (element. Value. (WordCount))    }}

  

You can also visit my Raspberry Pi blog address:

http://www.codeforfun.info/

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.