This is a creation in Article, where the information may have evolved or changed.
We know that a similar priority queue is implemented using the heap stack. The purpose of the priority queue I will not say more, is usually used to do the task of assigning weights.
The following Priority_queue priority libraries were found in github.com. Looked at his source code implementation, learned that he is not thread-safe. If you want to implement the thread safety of the data, we need to implement the global lock with sync lock to ensure the atomicity of the data.
The article wrote a little messy, welcome to spray! In addition, the article continues to update, please go to the original address to view the update.
http://xiaorui.cc/?p=2929
Python <textarea wrap= "soft" class= "Crayon-plain print-no" data-settings= "readonly style="-MOZ-TAB-SIZE:4; -o-tab-size:4; -webkit-tab-size:4; Tab-size:4; FONT-SIZE:12PX!important; line-height:15px!important; " > #xiaorui. Ccpackage Mainimport ("FMT" "Github.com/gansidui/priority_queue") type Node struct {priority Intvalue string} Func (this *node) less (other interface{}) bool {return this.priority < other. ( *node). Priority}func Main () {q: = priority_queue. New () Q.push (&node{priority:8, Value: "8"}) Q.push (&node{priority:7, Value: "7"}) Q.push (&node{priority:9 , Value: "9"}) Q.push (&node{priority:2, Value: "2"}) Q.push (&node{priority:4, Value: "4"}) Q.push (&node{ Priority:3, Value: "3"}) Q.push (&node{priority:5, Value: "5"}) x: = Q.top (). (*node) fmt. Println (x.priority, X.value) for Q.len () > 0 {x = Q.pop (). ( *node) fmt. Println (X.priority, X.value)}} </textarea>
1234567891011121314151617181920212223242526272829303132333435363738 |
#xiaorui. CC PackageMainImport ("FMT""Github.com/gansidui/priority_queue")type Node struct { Priorityintvaluestring}func ( This *Node) Less( Other Interface{}) BOOL {return This. Priority < Other.(*Node). Priority}funcMain() {Q := priority_queue.New()Q.Push(&Node{ Priority: 8, value: "8"})Q.Push(&Node{ Priority: 7, value: "7"})Q.Push(&Node{ Priority: 9, value: "9"})Q.Push(&Node{ Priority: 2, value: "2"})Q.Push(&Node{ Priority: 4, value: "4"})Q.Push(&Node{ Priority: 3, value: "3"})Q.Push(&Node{ Priority: 5, value: "5"})x := Q.Top().(*Node)FMT.Println(x. Priority, x.value) for Q.Len() > 0 {x = Q.Pop().(*Node)FMT.Println(x. Priority, x.value)}} |