Java数据结构(四):线性表之双向链表

java实现简单的双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。 所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。 一般我们都构造双向循环链表。

具体代码可参见:BidirectionalLinkedList.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package datastructure.linear.linked;

import datastructure.exception.StructureException;
import datastructure.linear.AbstractList;

/**
* @Description 双向链表实现
* @author mastery
* @Date 2015年6月30日下午9:11:51
*/
public class BidirectionalLinkedList<T> extends AbstractList<T> {

class Node<T1> {
/**
* 数据域
*/
T1 element;

/**
* 指向前驱结点的指针
*/
Node<T1> prior;

/**
* 指向后继结点的指针
*/
Node<T1> next;

public Node(Node<T1> next) {
super();
this.prior = next;
this.next = next;
}

public Node(T1 element, Node<T1> prior, Node<T1> next) {
super();
this.element = element;
this.prior = prior;
this.next = next;
}

}

private Node<T> head;

private Node<T> currentNode;

public BidirectionalLinkedList() {
head = currentNode = new Node<T>(null);
size = 0;
}

/**
* 得到当前下标对应的结点
*
* @param index
* @throws StructureException
*/
public void indexNodeToCurrent(int index) throws StructureException {
currentNode = head;
if (index < -1 || index > size - 1) {
throw new StructureException("index参数异常!");
}
if (index == -1) {
return;
}
currentNode = head.next;
int j = 0;
while (currentNode != null && j < index) {
currentNode = currentNode.next;
j++;
}
}

@Override
public void insert(int index, T t) throws StructureException {
if (index < 0 || index > size) {
throw new StructureException("index参数异常!");
}
// 得到当前下标的上一个结点
indexNodeToCurrent(index - 1);
Node<T> insertNode = new Node<T>(t, currentNode, currentNode.next);
if (currentNode.next != null) {
// 将新元素生成结点插入到当前结点下
currentNode.next.prior = insertNode;
}
currentNode.next = insertNode;
size++;
}

@Override
public void delete(int index) throws StructureException {
if (isEmpty()) {
throw new StructureException("链表为空");
}
if (index < 0 || index > size) {
throw new StructureException("index参数异常");
}
indexNodeToCurrent(index - 1);
Node<T> twoNextNode = currentNode.next.next;
if (twoNextNode != null) {
twoNextNode.prior = currentNode;
}
currentNode.next = twoNextNode;
size--;
}

@Override
public T get(int index) throws StructureException {
if (isEmpty()) {
throw new StructureException("链表为空");
}
if (index < 0 || index > size) {
throw new StructureException("index参数异常!");
}
indexNodeToCurrent(index);
return currentNode.element;
}

}