双向链表
1构造方法
public DoublyLinkedList() {
this.first = new Node();
this.last = new Node(null, null, first);
this.first.next = this.last;
}
2链表是否为空
public boolean isEmpty() {
return first.next == last && last.prev == first;
}
3从前向后插入节点
Node node = new Node(item, this.first.next, this.first);
this.first.next = node;
node.next.prev = node;
4从后向前插入节点
public void afterList(String item) {
Node node = new Node(item, this.last, this.last.prev);
this.last.prev.next = node;
this.last.prev = node;
}
5获取双向链表中有效节点的个数
public int elementsCount() {
Node currentNode;
int cnt = 0;
for (currentNode = this.first.next; ; currentNode = currentNode.next) {
cnt++;
if (currentNode.next == last)
break;
}
return cnt;
}
6获取指定节点
public Node getIndexOfList(int index) {
Node currentNode = this.first.next;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
return currentNode;
}
7获取指定节点的内容,并把节点从指定位置删除
public String getByIndex(int index) {
if (this.isEmpty()) {
throw new EmptyList("链表为空");
}
if (index < 0 || index >= this.elementsCount()) {
throw new EmptyList("不存在"+index+"的元素");
}
Node targetNode = this.getIndexOfList(index);
String result = targetNode.item;
targetNode.next.prev = targetNode.prev;
targetNode.prev.next = targetNode.next;
targetNode.next = null;
targetNode.prev = null;
return result;
}
8打印链表中的内容
public void print() {
if (isEmpty())
System.out.println("[]");
StringBuilder stringBuilder = new StringBuilder();
Node node;
stringBuilder.append("[--");
for (node = this.first.next; ; node = node.next) {
stringBuilder.append(node.item + "--");
if (node.next == last) {
stringBuilder.append("]");
break;
}
}
System.out.println(stringBuilder.toString());
}
9节点的静态内部类
public static class Node {
public Node() {
}
public Node(String item) {
this.item = item;
}
public Node(String item, Node next, Node prev) {
this.item = item;
this.next = next;
this.prev = prev;
}
private String item;
private Node next;
private Node prev;
}
}
10异常处理
class EmptyList extends RuntimeException{
public EmptyList() {
}
public EmptyList(String msg)
{
super(msg);
}
}
总体呈现
package com.yu;
public class DoublyLinkedList {
public DoublyLinkedList() {
this.first = new Node();
this.last = new Node(null, null, first);
this.first.next = this.last;
}
private Node first;
private Node last;
public boolean isEmpty() {
return first.next == last && last.prev == first;
}
public void beforeList(String item) {
Node node = new Node(item, this.first.next, this.first);
this.first.next = node;
node.next.prev = node;
}
public void afterList(String item) {
Node node = new Node(item, this.last, this.last.prev);
this.last.prev.next = node;
this.last.prev = node;
}
public int elementsCount() {
Node currentNode;
int cnt = 0;
for (currentNode = this.first.next; ; currentNode = currentNode.next) {
cnt++;
if (currentNode.next == last)
break;
}
return cnt;
}
public Node getIndexOfList(int index) {
Node currentNode = this.first.next;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
return currentNode;
}
public String getByIndex(int index) {
if (this.isEmpty()) {
throw new EmptyList("链表为空");
}
if (index < 0 || index >= this.elementsCount()) {
throw new EmptyList("不存在"+index+"的元素");
}
Node targetNode = this.getIndexOfList(index);
String result = targetNode.item;
targetNode.next.prev = targetNode.prev;
targetNode.prev.next = targetNode.next;
targetNode.next = null;
targetNode.prev = null;
return result;
}
public void print() {
if (isEmpty())
System.out.println("[]");
StringBuilder stringBuilder = new StringBuilder();
Node node;
stringBuilder.append("[--");
for (node = this.first.next; ; node = node.next) {
stringBuilder.append(node.item + "--");
if (node.next == last) {
stringBuilder.append("]");
break;
}
}
System.out.println(stringBuilder.toString());
}
public static class Node {
public Node() {
}
public Node(String item) {
this.item = item;
}
public Node(String item, Node next, Node prev) {
this.item = item;
this.next = next;
this.prev = prev;
}
private String item;
private Node next;
private Nocde prev;
}
}
class EmptyList extends RuntimeException{
public EmptyList() {
}
public EmptyList(String msg)
{
super(msg);
}
}
1测试类
package com.yu;
public class Demo1 {
public static void main(String[] args) {
DoublyLinkedList linkedList = new DoublyLinkedList();
System.out.println(linkedList.isEmpty());
linkedList.afterList("宫本0");
linkedList.afterList("宫本1");
linkedList.afterList("宫本2");
linkedList.afterList("宫本3");
linkedList.afterList("宫本4");
System.out.println(linkedList.getByIndex(1));
linkedList.print();
System.out.println(linkedList.elementsCount());
System.out.println(linkedList.isEmpty());
}
}