java優先隊列

優先隊列的寫法有好多種,其中包括二叉樹的寫法,不過本人對於二叉樹還沒有喫透,不能隨便就傳授給大家,本文的方法跟前兩篇目的是一樣的,儘量不用第三方庫,純java數據處理。

下面是隊列的屬性與結構,我用鍵值對的方式去聲明該數據的優先級:

 

package com.example.a;

public class Element {
	private Object element; // 數據
	private int priority; // 優先級

	public Element(Object obj, int priority) {
		this.element = obj;
		this.priority = priority;
	}

	public Object getElement() {
		return element;
	}

	public void setElement(Object element) {
		this.element = element;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

}

 

 

下面是隊列的實現,註釋也很清楚:

package com.example.a;

public class PrioritySequenceQueue implements Queue {

	int front = 0; // 隊頭
	int rear = 0; // 隊尾
	int count = 0; // 計數器
	int maxSize = 10; // 隊列最大長度
	Element[] queue = new Element[maxSize]; // 隊列

	public PrioritySequenceQueue() {

	}

	@Override
	public void append(Object obj) throws Exception {
		// TODO Auto-generated method stub
		// 如果隊列已滿
		if (count >= maxSize) {
			throw new Exception("隊列已滿!");
		}
		queue[rear] = (Element) obj;
		rear++;
		count++;
	}

	@Override
	public Object delete() throws Exception {
		// TODO Auto-generated method stub
		if (isEmpty()) {
			throw new Exception("隊列爲空!");
		}
		// 默認第一個元素爲優先級最高的。
		Element min = queue[0];
		int minIndex = 0;
		for (int i = 0; i < count; i++) {
			if (queue[i].getPriority() < min.getPriority()) {
				min = queue[i];
				minIndex = i;
			}
		}

		// 找的優先級別最高的元素後,把該元素後面的元素向前移動。
		for (int i = minIndex + 1; i < count; i++) {
			queue[i - 1] = queue[i]; // 移動元素
		}
		rear--;
		count--;
		return min;
	}

	@Override
	public Object getFront() throws Exception {
		// TODO Auto-generated method stub
		if (isEmpty()) {
			throw new Exception("隊列爲空!");
		}
		// 默認第一個元素爲優先級最高的。
		Element min = queue[0];
		int minIndex = 0;
		for (int i = 0; i < count; i++) {
			if (queue[i].getPriority() < min.getPriority()) {
				min = queue[i];
				minIndex = i;
			}
		}
		return min;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return count == 0;
	}

}

 

下面是接口:

package com.example.a;

public interface Queue {
	// 入隊
	public void append(Object obj) throws Exception;

	// 出隊
	public Object delete() throws Exception;

	// 獲得隊頭元素
	public Object getFront() throws Exception;

	// 判斷對列是否爲空
	public boolean isEmpty();

}

 

 


下面是測試:

 

 

 

package com.example.a;

public class Test {
	public static void main(String[] args) throws Exception {

		PrioritySequenceQueue queue = new PrioritySequenceQueue();
		Element temp;

		// 五個進程入隊
		queue.append(new Element(1, 30));
		queue.append(new Element(2, 20));
		queue.append(new Element(3, 40));
		queue.append(new Element(4, 20));
		queue.append(new Element(5, 0));

		// 按照優先級出隊。
		System.out.println("編號  優先級");
		while (!queue.isEmpty()) {
			temp = (Element) queue.delete();
			System.out.println(temp.getElement() + " " + temp.getPriority());
		}
	}

}


輸出結果:

 

編號  優先級
5 0
2 20
4 20
1 30
3 40

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章