转:https://blog.csdn.net/javazejian/article/details/53375004
1.Comparable接口,实现类的比较
可实现.comareTo方法
2.自定义队列类
package myproject.work.Queue_demo;
/**
* 队列类
*/
public class Process implements Comparable<Process> {//实现Comparable接口 能够进行比较
private String name;//进程名称
private int priority;//进程优先级默认5,范围1~10 实际上比较的int类型
public Process(String name, int priority) {
this.name = name;
if (priority >= 1 && priority <= 10) {
this.priority = priority;
} else {
throw new IllegalArgumentException("priority must between 1 and 10");
}
}
public Process(String name) {
this(name, 5);
}
/**
* 优先级比较
*
* @param o
* @return
*/
@Override
public int compareTo(Process o) {
return this.priority - o.priority;
}
@Override
public String toString() {
return "Process{" +
"name='" + name + '\'' +
", priority=" + priority +
'}';
}
public static void main(String[] args) {
Process process = new Process("进程 1", 3);
Process process1 = new Process("进程 2", 4);
System.out.println(process.compareTo(process1));//-1
System.out.println(process1.compareTo(process));//1
System.out.println(process.compareTo(process));//0
}
}
3.以双链表为基底 实现顺序排列的链表
package myproject.work.Queue_demo;
import java.io.Serializable;
/**
*
* 顺序队列
*
* 这里使用之前分析过的MyLikedList作为基底,
* 实现一个排序的SortLinkedList继承自MyLinkedList,
* 这里需要注意的是排序链表中的T类型必须是实现了Comparable接口的类型,
* 在SortLinkedList中主要重写添加的add方法,插入逻辑是,通过比较元素的大小加入,而非简单下标或尾部插入
* @param <T>
*/
public class SortMyLinkedList<T extends Comparable<? extends T>> extends MylinkeList<T> implements Serializable {
@Override
public boolean add(T data){
//转换类型 否则idea编译不过
Comparable cmp=data;
//判断是否是从尾部加入的数据 从尾部插入
if (this.isEmpty() || cmp.compareTo(this.last.prev.data) > 0) {//排序
return super.add(data);//从尾部直接添加
}
//不是从尾部插入
Node<T> p = this.first;
//找到插入点的前一位
while (p.next!=null&&cmp.compareTo(p.next.data)>0){
p=p.next;
}
//改变指针
Node<T> tNode = new Node<T>(p, data, p.next);
p.next=tNode;
p.next.prev=tNode;
modCount++;
size++;
return true;
}
//测试
public static void main(String[] args){
SortMyLinkedList<Integer> list=new SortMyLinkedList<>();
list.add(50);
list.add(40);
list.add(80);
list.add(20);
print(list);
}
public static void print(SortMyLinkedList mylinkeList){
for (int i=0;i<mylinkeList.size();i++) {
System.out.println("i->"+mylinkeList.get(i));
}
}
}
3.以排序链表的为基底 实现优先队列
package myproject.work.Queue_demo;
import java.io.Serializable;
import java.util.NoSuchElementException;
/**
* 优先队列的简单实现,采用排序双链表,T必须实现Comparable接口
* @param <T>
*/
public class PriorityQueue<T extends Comparable<? extends T>> implements Queue<T>,Serializable {
//排序循环的双链表
private SortMyLinkedList<T> list;
//true表示 升序 false表示 降序
private boolean asc;
//最大容量
private int maxSize = 128;
//初始化队列
public PriorityQueue(boolean asc) {
this.list = new SortMyLinkedList<>();
this.asc = asc;//默认为升序
}
//获取最大容量
public int getMaxSize() {
return this.maxSize;
}
//设置最大的数量
public void setMaxSize(int size) {
this.maxSize = size;
}
/**
* 获取对像数量
*
* @return
*/
@Override
public int size() {
return this.list.size;
}
/**
* 判空
*
* @return
*/
@Override
public boolean isEmpty() {
return this.list.isEmpty();
}
/**
* 添加队列数据
*
* @param data
* @return
*/
@Override
public boolean add(T data) {
return list.add(data);
}
/**
* 添加元素 加上异常
*
* @param data
* @return
*/
@Override
public boolean offer(T data) {
if (data == null)
throw new NullPointerException("The data can\'t be null");
if (list.size() >= maxSize)
throw new IllegalArgumentException("The capacity of PriorityQueue has reached its maxSize:128");
return add(data);
}
/**
* 获取元素 不进行删除操作
*
* @return
*/
@Override
public T peek() {
return this.asc ? list.get(0) : list.get(size() - 1);
}
/**
* 获取元素 进行异常
*
* @return
*/
@Override
public T element() {
if (isEmpty()) {
throw new NoSuchElementException("为空");
}
return peek();
}
/**
* 出队 获取元素 删除元素
*
* @return
*/
@Override
public T poll() {
if (isEmpty()) {
return null;
}
return this.asc ? list.remove(0) : list.remove(size() - 1);
}
@Override
public T remove() {
if (isEmpty()) {
throw new NoSuchElementException("weikon");
}
return poll();
}
@Override
public void clearQueue() {
this.list.clear();
}
//测试
public static void main(String[] args) {
PriorityQueue<Process> priorityQueue = new PriorityQueue<>(false);
System.out.println("初始化队列");
priorityQueue.add(new Process("进程1", 10));
priorityQueue.add(new Process("进程2", 1));
priorityQueue.add(new Process("进程3", 8));
priorityQueue.add(new Process("进程4", 3));
priorityQueue.add(new Process("进程5"));
System.out.println("队列中的进程执行优先级:");
while (!priorityQueue.isEmpty()) {
System.out.println("process:" + priorityQueue.poll().toString());
}
}
}