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 ())}}
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/