链表-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);
}
}
/**
* 有序链表
* @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
- 链表-F-有序链表
- 【链表】合并两个有序链表,合并后链表依旧有序
- 链表-有序链表操作
- 有序数组 2F查找
- 有序的合并两个有序链表
- 两个有序链表合并为有序
- 有序链表合并&有序数组合并
- (十一)链表—有序链表
- 链表------两个有序链表的合并
- 链表E 有序链表的归并
- 有序链表合并
- 合并有序链表
- 有序链表
- 合并有序链表
- 合并有序链表
- 合并有序链表
- 有序链表合并
- 合并有序链表
- 链表-E-实现队列
- 计算机操作系统知识图谱
- 数论(多个数gcd) CodeForces757B Bash's Big Day
- CentOS 7下MySQL 5.7安装、配置与应用
- 226. Invert Binary Tree
- 链表-F-有序链表
- 链表-G- 利用有序链表给数组排序
- Unity3d调用Anroid(Android Studio环境)方法
- Javascript如何实现二分查找?
- Log4net配置与使用简要说明
- 链表-H-双向链表,允许前后遍历
- 高质量的SRS的一些特性
- 链表-I-链表实现迭代器
- MySQL数据库的子查询