首页 > 代码库 > 有序链表实现的优先级队列

有序链表实现的优先级队列

package day1_29;public class Link {	public long dDate;	public Link next;		public Link(long dDate){		this.dDate = dDate;	}		//打印链结点的方法	public void displayLink(){				System.out.println("["+dDate+"]");			}	}==================================================package day1_29;public class SortedList {	private Link first;		public SortedList(){		first = null;	}		//判断链表是不是为空	public boolean isEmpty(){		if(first == null){			return true;		}else{			return false;		}	}		//插入一个元素的实现	public void insert(long key){		Link newLink = new Link(key);		Link current = first;		Link previous = null;		while(current != null && key >current.dDate){			previous = current;			current = current.next;				}		if(previous == null){//在头节点的时候			first = newLink;		}else			previous.next = newLink;				newLink.next = current;		}		//删除节点的实现	public Link remove(){//删除头结点的方法,只能删除头节点,为了符合队列的性质		Link temp = first;		first = first.next;		return temp;					}		//打印链表的实现	public void displayList(){				Link current = first;		while(current != null){									current.displayLink();			current = current.next;					}		System.out.println();											}										}================================================package day1_29;public class PriorityQ {	private SortedList sortedList;		public PriorityQ(){		sortedList = new SortedList();			}		//队列的插入方法	public void insert(long item){		sortedList.insert(item);	}		//删除的方法	public long remove(){				return sortedList.remove().dDate;	}		//判断是不是为空	public boolean isEmpty(){		return sortedList.isEmpty();	}		//打印的方法	public void display(){		sortedList.displayList();	}							}==========================================package day1_29;public class Practice5_1 {	/**	 * 实现一个基于有序链表的优先级队列,队列的删除操作应该删除具有最小关键字	 * 的链结点	 */	public static void main(String[] args) {		PriorityQ priorityQ = new PriorityQ();		priorityQ.insert(11);		priorityQ.insert(33);		priorityQ.insert(22);		priorityQ.insert(55);		priorityQ.insert(44);				priorityQ.display();	}}

  

有序链表实现的优先级队列