Skip to content

Emove/pq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pq

一个基于 Go 泛型实现的 优先队列 (Priority Queue),支持大顶堆和小顶堆模式。

Go Reference
License: MIT


📦 安装

go get github.com/emove/pq

🚀 快速开始

package main

import (
    "fmt"
    "github.com/Emove/pq"
)

func main() {
    // 创建一个小顶堆(默认)
    q := pq.New[int]()
	// 创建一个大顶堆
	// q := pq.New[int](pq.MaxHeap()) 

    // 插入元素
    q.Insert(10, 3.0)
    q.Insert(20, 1.0)
    q.Insert(30, 2.0)

    fmt.Println("队列长度:", q.Len()) // 3

    // 弹出优先级最高(最小值)的元素
    v, _ := q.Pop()
    fmt.Println("Pop:", v) // 输出 20,因为它的 priority=1.0 最小

    // 更新某个元素的优先级
    q.UpdatePriority(30, 0.5)

    v, _ = q.Pop()
    fmt.Println("Pop:", v) // 输出 30,因为更新后 priority=0.5
}

About

A priority queue implemented in Go generics, supporting both min-heap and max-heap modes.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages