Java数据结构和算法(一)线性结构之单链表

     prev                 current              next
-------------- -------------- --------------
| value | next | -> | value | next | -> | value | next |
-------------- -------------- --------------

单链表的结构如上:最后一个节点的 next=null。下面看一下代码。

(1) 链表的基本操作

public class Node<E> {

    private E value;
private Node next; public Node(E value) {
this.value = value;
} // 追加到最后一个元素
public Node append(Node node) {
Node tail = tail();
tail.next(node);
return this;
} // 删除指定的节点
public void remove(Node node) {
Node prev = prev(node);
if (prev != null) {
prev.next = node.next;
}
} // 节点总数
public int size() {
int size = 1;
Node current = this;
while (current.next != null) {
size++;
current = current.next;
}
return size;
} // 查找指定节点的上一个节点
public Node prev(Node node) {
Node prev = this;
while (prev != null) {
if (prev.next == node) {
return prev;
}
prev = prev.next;
}
return null;
} // 查找尾节点,单链表 tail.next=null
public Node tail() {
Node tail = this;
while (tail.next != null) {
tail = tail.next;
}
return tail;
} // 设置当前节点的下一个节点
public void next(Node next) {
// 设置该节点的后继节点
next.next = this.next;
// 将该节点设置为当前节点的前驱节点
this.next = next;
} public Node next() {
return next;
} public E getValue() {
return value;
} public void setValue(E value) {
this.value = value;
}
}

(2) 取出中间节点

偶数节点取中间两个节点的前一个节点,奇数节点取正中间的节点

public Node mid() {
Node stepOneNode = this;
Node stepTwoNode = this;
while (stepTwoNode != null) {
stepTwoNode = stepTwoNode.next;
if (stepTwoNode != null) {
stepTwoNode = stepTwoNode.next;
if (stepTwoNode != null) {
stepOneNode = stepOneNode.next;
}
}
}
return stepOneNode;
}

(3) 链表反转

public Node reverse() {
Node prev = null;
Node next = null;
Node current = this;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}

测试一把:

public void test1() {
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3); n1.append(n2).append(n3);
Assert.assertEquals(3, n1.next().next().getValue());
Assert.assertEquals(3, n1.tail().getValue());
Assert.assertEquals(2, n1.prev(n3).getValue());
Assert.assertEquals(3, n1.size()); n1.remove(n2);
Assert.assertEquals(3, n1.next().getValue());
Assert.assertEquals(1, n1.mid().getValue()); n1.next(n2);
Assert.assertEquals(3, n1.next().next().getValue());
Assert.assertEquals(2, n1.mid().getValue()); Node reverse = n1.reverse();
Assert.assertEquals(3, reverse.getValue());
Assert.assertEquals(2, reverse.next().getValue());
Assert.assertEquals(1, reverse.next().next().getValue());
}

(4) 有序链表的合并

两个有序链表合并后还是有序的,代码如下:

// 有序链表合并,两个链表均升序排列,最终的结果也升序排列
public static Node merge(Node<Integer> node1, Node<Integer> node2) {
if (node1 == null || node2 == null) {
return node1 == null ? node2 : node1;
} Node<Integer> head = node1.value < node2.value ? node1 : node2;
Node<Integer> cur1 = head == node1 ? node1 : node2; // 小
Node<Integer> cur2 = head == node1 ? node2 : node1; // 大 Node prev = null; // curl1 的前驱节点,小
while (cur1 != null && cur2 != null) {
if (cur1.value < cur2.value) {
prev = cur1;
cur1 = cur1.next;
} else {
// 将 curl2 插入到 prev 和 curl1 之间
Node tmp = cur2.next;
cur2.next = cur1;
prev.next = cur2;
prev = cur2;
cur2 = tmp;
}
}
prev.next = cur1 == null ? cur2 : cur1;
return head;
} // 有序链表合并,两个链表均升序排列,最终的结果也升序排列
public static Node mergeRecurse(Node<Integer> node1, Node<Integer> node2) {
if (node1 == null || node2 == null) {
return node1 != null ? node1 : node2;
}
Node head = null;
if (node1.value > node2.value) {
head = node2;
head.next = mergeRecurse(node1, node2.next);
} else {
head = node1;
head.next = mergeRecurse(node1.next, node2);
}
return head;
}

测试:

public void mergeTest() {
Node n1 = new Node(1);
// 省略...
Node n6 = new Node(6); n1.append(n3).append(n5);
n2.append(n4).append(n6); Node merge1 = Node.merge(n1, n2);
//Node merge1 = Node.mergeRecurse(n1, n2);
Assert.assertEquals(6, merge1.size());
Assert.assertEquals(2, merge1.next().getValue());
}

每天用心记录一点点。内容也许不重要,但习惯很重要!

Java数据结构和算法(一)线性结构之单链表的更多相关文章

  1. [数据结构 - 第3章] 线性表之单链表(C++实现)

    一.类定义 单链表类的定义如下: #ifndef SIGNALLIST_H #define SIGNALLIST_H typedef int ElemType; /* "ElemType类型 ...

  2. Java数据结构与算法(1):线性表

    线性表是一种简单的数据类型,它是具有相同类型的n个数据元素组成的有限序列.形如如A0,A1,...,An-1.大小为0的表为空表,称Ai后继Ai-1,并称Ai-1前驱Ai. printList打印出表 ...

  3. Java数据结构和算法(一)线性结构

    Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...

  4. 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现

      本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型   栈是 ...

  5. java数据结构与算法之栈(Stack)设计与实现

    本篇是java数据结构与算法的第4篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是一种用于 ...

  6. Java数据结构和算法 - 二叉树

    前言 数据结构可划分为线性结构.树型结构和图型结构三大类.前面几篇讨论了数组.栈和队列.链表都是线性结构.树型结构中每个结点只允许有一个直接前驱结点,但允许有一个以上直接后驱结点.树型结构有树和二叉树 ...

  7. Java数据结构和算法 - 数组

    Q: 数组的创建? A: Java中有两种数据类型,基本类型和对象类型,在许多编程语言中(甚至面向对象语言C++),数组也是基本类型.但在Java中把数组当做对象来看.因此在创建数组时,必须使用new ...

  8. Java数据结构和算法(一)树

    Java数据结构和算法(一)树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 前面讲到的链表.栈和队列都是一对一的线性结构, ...

  9. Java数据结构和算法 - 堆

    堆的介绍 Q: 什么是堆? A: 这里的“堆”是指一种特殊的二叉树,不要和Java.C/C++等编程语言里的“堆”混淆,后者指的是程序员用new能得到的计算机内存的可用部分 A: 堆是有如下特点的二叉 ...

随机推荐

  1. 好用的 FTP 软件之 FileZilla 技巧教程

    FTP 软件之 FileZilla教程 使用教程参考:http://163.26.161.1/~yilinteacher/wwwict/flash/FileZilla.swf (1)如何设置传输完成后 ...

  2. angularjs 整合 bootstrap

    第一步 :下载 bootstrap  jquery  ppper.js npm install bootstrap@4.0.0-beta.2 jquery popper.js --save 第二步: ...

  3. Node + H5 + WebSocket + Koa2 实现简单的多人聊天

    服务器代码  ( 依赖于 koa2,  koa-websocket ) /* 实例化外部依赖 */ let Koa = require("koa2"); let WebSocket ...

  4. jsfl 改变舞台宽高

    fl.getDocumentDOM().height= 680; fl.getDocumentDOM().width= 550;

  5. jsfl 第一天

    ctrl+f10,打开操作记录 通过打开到目标fla的舞台,然后运行写好的jsfl,默认就以目标fla为调试对象. 通过name可以获取层名字,帧标签,元件实例名等,name属性要根据对象的应用而产生 ...

  6. import 语句用于导入从外部模块,另一个脚本等导出的函数,对象或原语。

    import 语句用于导入从外部模块,另一个脚本等导出的函数,对象或原语. 注意:此功能目前无法在任何浏览器中实现.它在许多转换器中实现,例如 Traceur Compiler , Babel , R ...

  7. JAVA NIO学习记录2-非阻塞式网络通信

    一.阻塞与非阻塞 传统的IO 流都是阻塞式的.也就是说,当一个线程调用read() 或write() 时,该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务.因此,在完成网络通信 ...

  8. linux——DNS服务器配置

    讲课,请不要在课堂上查找文件,浏览器搜索,会感觉你很不上心,玩听众,一定提前做很多遍,模拟很多遍: 演讲,请务必准备好材料,考虑听众的感受,一定不要让外行人云里雾里,听不懂你在讲什么,那就尴尬了, D ...

  9. jQuery:总体掌握

    链式编程....方法多,属性无法得到对象进行链式.vs10自动完成.书籍锋利的jQuery vsdoc有智能提示开发时候用,开发完之后,换成min压缩版的. 经验:打开网站文件夹.可以把vs网站上的解 ...

  10. CTR点击率预估干货分享

    CTR点击率预估干货分享 http://blog.csdn.net/bitcarmanlee/article/details/52138713