链表-F-有序链表

来源:互联网 发布:pythonpath linux 编辑:程序博客网 时间:2024/06/05 18:16
package e_link.F_sortedList;
/**
 * 有序链表
 * @author Administrator
 *
 */
public class SortedListApp {
public static void main(String[] args) {
SortedList theSort = new SortedList();
theSort.insert(10);
theSort.insert(80);
theSort.displayList();
theSort.insert(5);
theSort.insert(30);
System.out.println("-----");
theSort.displayList();


theSort.remove();
theSort.remove();
theSort.remove();
System.out.println("-----");
theSort.displayList();
}
}package e_link.F_sortedList;


public class SortedList {
public Link first;


public SortedList() {
first = null;
}


public boolean isEmpty() {
return first == null;
}


public void insert(long key) {
Link newLink = new Link(key);
Link previous = null;
Link current = first;
while (current != null && key > current.data) {
previous = current;
current = current.next;
}
if (previous == null) {
first = newLink;
} else {
previous.next = newLink;
}
newLink.next = current;
}


public Link remove() {
Link link = first;
first = first.next;
return link;
}


public void displayList() {
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}


}


}package e_link.F_sortedList;


public class Link {
public long data;
public Link next;


public Link(long data) {
this.data = data;
}
public void displayLink(){
System.out.println(data);
}


}
0 0