标签:
1:问题描述是,实现一个基于有序链表的优先级队列,队列的删除操作应该具有最小关键字的链结点。
package 链表实践; public class Link { public long dData; public Link next; public Link(long dd) { dData = dd; } public void displayLink() { System.out.print(dData+" "); } } -------------------------------------------------------------------------- package 链表实践; 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 previous = null;//从first开始 Link current = first; while(current != null && key > current.dData) { 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() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while (current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } } --------------------------------------------------------------- package 链表实践; 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().dData; } public boolean isEmpty() { return sortedList.isEmpty(); } public void display() { sortedList.displayList(); } }
标签:
原文地址:http://www.cnblogs.com/aicpcode/p/4195601.html