优先级队列

复制代码
//优先级队列
/*1先进先出
2按照优先级进行存储
3 优先级高的能出队*/
public class TestPrioLink {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PrioLink p=new PrioLink();
        p.push(111,6 );
        p.push(222,4);
        p.push(333,1);
        p.show();
    }

}
class PrioLink{
    class Entry{
        int data;
        Entry next;
        int prio;//优先级
    
        public Entry(){
           int data=-1;
           next=null;
           prio=-1;
        }
        public Entry(int data,int prio){
           this.data=data;
           this.next=null;
           this.prio=prio;
        }
    }
    private Entry head=null;
    public PrioLink(){
        this.head=new Entry();
    }
    
    //插入  按照优先级插入
    public void push(int data,int prio){
        Entry cur=this.head;
        for(;cur.next!=null;cur=cur.next){
            if(cur.next.prio>prio){
                break;
            }
        }
            Entry entry=new Entry(data,prio);
            entry.next=cur.next;
            cur.next=entry;
        }
    //     出队   头结点后一个
    public void pop(){
        Entry cur=this.head;
        if(cur.next==null){
            return;
        }
        Entry entry=cur.next;
        cur.next=entry.next;
        entry=null;
    }
    public void show(){
        Entry cur=this.head.next;
        while(cur!=null){
            System.out.println(cur.data+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}
复制代码

 

posted on   番茄疯了  阅读(104)  评论(0)    收藏  举报

编辑推荐:
· 于是转身独立开发者
· C#.Net筑基-泛型T & 协变逆变
· dotnet 代码调试方法
· DbContext是如何识别出实体集合的
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
阅读排行:
· 免费开源 .NET OpenCV 迷你运行时全平台发布
· 10亿订单如何分库分表?
· 一个static关键字引发的线上故障:深度剖析静态变量与配置热更新的陷阱
· C# 的深度强化学习框架RL_Matrix
· 如何基于three.js(webgl)引擎架构,实现3D医院、3D园区导航,3D科室路径导航
< 2025年7月 >
29 30 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

统计

点击右上角即可分享
微信分享提示