java实现双向链表(附完整源码)
时间: 2023-11-11 10:04:18 浏览: 87
Java实现双向链表
下面是Java实现双向链表的完整源码:
```java
public class DoublyLinkedList<T> {
private Node head;
private Node tail;
private int size;
public DoublyLinkedList() {
head = null;
tail = null;
size = 0;
}
private class Node {
T data;
Node prev;
Node next;
public Node(T data) {
this.data = data;
prev = null;
next = null;
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void addFirst(T data) {
Node newNode = new Node(data);
if (isEmpty()) {
tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
}
head = newNode;
size++;
}
public void addLast(T data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
}
tail = newNode;
size++;
}
public void add(int index, T data) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
if (index == 0) {
addFirst(data);
return;
}
if (index == size) {
addLast(data);
return;
}
Node current = head;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
Node newNode = new Node(data);
newNode.prev = current;
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
size++;
}
public T removeFirst() {
if (isEmpty()) {
throw new RuntimeException("Empty list!");
}
T data = head.data;
head = head.next;
if (head == null) {
tail = null;
} else {
head.prev = null;
}
size--;
return data;
}
public T removeLast() {
if (isEmpty()) {
throw new RuntimeException("Empty list!");
}
T data = tail.data;
tail = tail.prev;
if (tail == null) {
head = null;
} else {
tail.next = null;
}
size--;
return data;
}
public T remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
if (index == 0) {
return removeFirst();
}
if (index == size - 1) {
return removeLast();
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
size--;
return current.data;
}
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
public void set(int index, T data) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
current.data = data;
}
public boolean contains(T data) {
Node current = head;
while (current != null) {
if (current.data.equals(data)) {
return true;
}
current = current.next;
}
return false;
}
public void clear() {
head = null;
tail = null;
size = 0;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
Node current = head;
while (current != null) {
sb.append(current.data);
if (current.next != null) {
sb.append(", ");
}
current = current.next;
}
sb.append("]");
return sb.toString();
}
}
```
代码中使用了嵌套类Node表示双向链表中的每个节点,包含一个数据域和两个指针域(prev和next),分别指向前驱节点和后继节点。
程序首先定义了一个DoublyLinkedList类,包含头节点head、尾节点tail和链表长度size三个属性。然后,程序实现了以下方法:
- size:返回链表长度
- isEmpty:判断链表是否为空
- addFirst:在链表头部插入一个元素
- addLast:在链表尾部插入一个元素
- add:在指定位置插入一个元素
- removeFirst:删除链表头部元素
- removeLast:删除链表尾部元素
- remove:删除指定位置的元素
- get:获取指定位置的元素
- set:设置指定位置的元素
- contains:判断链表中是否包含指定元素
- clear:清空链表
- toString:将链表转换为字符串形式
以上是Java实现双向链表的完整源码,希望能对你有所帮助。
阅读全文