用java实现优先级别队列
来源:互联网 发布:windows sublime c 编辑:程序博客网 时间:2024/06/04 19:25
package com.test3;
public class Element {
private Object elem;//原先意义下的数据元素
private int priority;//优先级
public Element(){
}
public Element(Object elem, int priority){
this.elem = elem;
this.priority = priority;
}
public Object getElem() {
return elem;
}
public void setElem(Object elem) {
this.elem = elem;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}package com.test3;
public class SeqPQueue {
static final int defaultSize = 10;
int front;//对头
int rear;//队尾
int count;//计数器
int maxSize;//元素最大个数
Element[] data;
public SeqPQueue(){
this.initiate(defaultSize);
}
public SeqPQueue(int sz){
this.initiate(sz);
}
private void initiate(int sz) {
maxSize = sz;
front = rear = 0;
count = 0;
data = new Element[sz];
}
//从队列尾部插入对象(Element)
public void append(Object obj) throws Exception{
if(count > maxSize){
throw new Exception("full");
}
data[rear] = (Element)obj;
rear = rear + 1;
count++;
}
public Element delete() throws Exception{
if(count == 0){
throw new Exception("blank");
}
//priority越小优先级别越高
//将优先级别最高的数放到min中
Element min = data[0];
int minIndex = 0;
for(int i = 0; i < count; i++){
if(data[i].getPriority() < min.getPriority()){
min = data[i];
minIndex = i;
}
}
for(int i = minIndex+1; i < count; i++){
data[i-1] = da ta[i];
}
rear = rear-1;
count--;
return min;
}
//查找第一个元素
public Object getFront() throws Exception{
if(count == 0){
throw new Exception("blank");
}
Element min = data[0];
for(int i = 0; i < count; i++){
if(data[i].getPriority() < min.getPriority()){
min = data[i];
}
}
return min;
}
public boolean notEmpty(){
return count != 0;
}
}package com.test3;
public class Test {
public static void main(String args[]){
SeqPQueue q = new SeqPQueue();Element temp;
try {
//传进去的是Element对象
q.append(new Element(new Integer(1), 30));
q.append(new Element(new Integer(2), 20));
q.append(new Element(new Integer(3), 40));
q.append(new Element(new Integer(4), 20));
q.append(new Element(new Integer(5), 0));
System.out.println("进程号,优先级");
while(q.notEmpty()){
temp = q.delete();
System.out.print(temp.getElem()+" ");System.out.println(temp.getPriority());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 用java实现优先级别队列
- 用 C# 实现优先队列
- 用 C# 实现优先队列
- 用堆实现优先队列
- 用堆实现优先队列
- 用STL实现优先队列
- 用堆实现优先队列
- 用 C# 实现优先队列
- 用堆实现优先队列
- 用堆实现优先队列
- 用java实现二叉查找树、堆和优先队列
- Dijkstra 算法用优先队列的java实现
- 用java实现一个基于堆排序的优先队列
- java 之堆实现优先队列
- java使用堆结构实现优先队列
- 数据结构-堆实现优先队列(java)
- 优先队列及java实现和用法
- 优先队列之左式堆(JAVA实现)
- Appium入门--环境准备和实例
- 技术揭秘12306改造(二):探讨12306两地三中心混合云架构
- java飞机游戏小项目
- 计算文本文件中各个词(中英文)出现的频率
- 用Java实现单向链表
- 用java实现优先级别队列
- 考研成绩查询系统(jsp、mysql、java)
- 贪吃蛇游戏(java)
- java坦克游戏
- Eclipse打jar包,资源文件的读取
- MVC、JSP实现mysql的增删改查功能的封装和简陋的界面交互
- 使用struts2框架来实现CRUD(create、read、update、delete)
- IDEA加密文件Base64转换String传输以及报文摘要MD5防止恶意篡改
- iOS各大牛博客