链式队列与优先级队列

链式队列

package practice;
//链式队列
class TestLinkQueue {
    class Entry{
        int data;
        Entry next;
        public Entry (){
            data = -1;
            next = null;
        }
        public Entry(int data){
            this.data = data;
            next = null;
        }
    }
    private Entry front = null;
    private Entry rear = null;
    private int usedSize = 0;

    //是否为空
    public boolean isEmpty(){
        return this.usedSize == 0;
    }

    //入队
    public void insertTail(int val){
        if(isEmpty()){
            this.rear = new Entry(val);
            this.front = this.rear;
        }else{
            this.rear.next = new Entry(val);
            this.rear = this.rear.next;
        }
        this.usedSize++;
    }

    //出队
    public void pop(){
        if(isEmpty()){
            return;
        }
        Entry cur = this.front;//cur用来指向出队的元素,以便制空
        this.front = cur.next;
        cur.next = null;
        this.usedSize--;
    }

    //得到队头元素
    public int getTop(){
        if(isEmpty()){
            return -1;
        }
        return this.front.data;
    }

    //show
    public void show(){
        Entry cur = this.front;
        while(cur != null){
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }

}
    public class TestLinkQ {
    public static void main(String[] args) {
        TestLinkQueue t1 =new TestLinkQueue();
        t1.insertTail(10);
        t1.insertTail(20);
        t1.insertTail(30);
        System.out.print("队内元素:");
        t1.show();
        t1.pop();
        System.out.print("出队后队内元素:");
        t1.show();
        System.out.println("队头元素为:"+t1.getTop());
    }

}

这里写图片描述
这里写图片描述
这里写图片描述
优先级队列

package practice;
/*
 * 优先级队列:按照优先级进行存储数据
 * */
class Priolink1{
    class Entry{
        int data;
        int prio;
        Entry next;

        public Entry(){
            data = -1; 
            prio = -1;
            next = null;
        }
        public Entry(int data,int prio){
            this.data = data;
            this.prio = prio;
            this.next = null;
        }
    }
    private Entry head = null;
    public Priolink1(){
        this.head = new Entry();
    }

    //插入,按照优先级进行插入
    public void insert(int data,int prio){
        Entry entry = new Entry(data,prio);
        Entry cur = head;
        while(cur.next != null){
            if(cur.next.prio>prio){
                break;
            }
            cur = cur.next;
        }
        entry.next = cur.next;
        cur.next = entry;
    }


    //出队
    public void pop(){
        Entry cur = this.head;

    if(cur.next == null){
        return;
    }
    Entry del = cur.next;
    cur.next = del.next;
    del = null;
    }

    //show
    public void show(){
        Entry cur = this.head.next;
        while(cur != null){
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
public class PrioLink {

    public static void main(String[] args) {
        Priolink1 t1 = new Priolink1();
        t1.insert(1, 1);
        t1.insert(10, 10);
        t1.insert(5, 5);
        System.out.print("队内元素:");
        t1.show();
        t1.pop();
        System.out.print("出队后元素:");
        t1.show();
    }

}

这里写图片描述
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值