首页 > 代码库 > 链表总结实践1
链表总结实践1
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(); } }
链表总结实践1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。