用 java这种语言以测试驱动开发实现双链表的构建、插入、删除、查找、反转。
时间: 2023-10-19 13:10:05 浏览: 33
以下是一个基于测试驱动开发的双链表的实现:
```
public class DoublyLinkedList<T> {
private Node<T> head;
private Node<T> tail;
private int size;
public DoublyLinkedList() {
head = null;
tail = null;
size = 0;
}
public void insert(T data) {
Node<T> node = new Node<>(data);
if (isEmpty()) {
head = node;
tail = node;
} else {
tail.setNext(node);
node.setPrev(tail);
tail = node;
}
size++;
}
public void delete(T data) {
Node<T> current = head;
while (current != null) {
if (current.getData().equals(data)) {
if (current == head) {
head = current.getNext();
if (head != null) {
head.setPrev(null);
}
} else if (current == tail) {
tail = current.getPrev();
if (tail != null) {
tail.setNext(null);
}
} else {
current.getPrev().setNext(current.getNext());
current.getNext().setPrev(current.getPrev());
}
size--;
return;
}
current = current.getNext();
}
}
public Node<T> find(T data) {
Node<T> current = head;
while (current != null) {
if (current.getData().equals(data)) {
return current;
}
current = current.getNext();
}
return null;
}
public void reverse() {
Node<T> current = head;
Node<T> temp = null;
while (current != null) {
temp = current.getPrev();
current.setPrev(current.getNext());
current.setNext(temp);
current = current.getPrev();
}
temp = head;
head = tail;
tail = temp;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
private static class Node<T> {
private T data;
private Node<T> prev;
private Node<T> next;
public Node(T data) {
this.data = data;
this.prev = null;
this.next = null;
}
public T getData() {
return data;
}
public Node<T> getPrev() {
return prev;
}
public void setPrev(Node<T> prev) {
this.prev = prev;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
}
}
```
接下来是一些测试用例:
```
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class DoublyLinkedListTest {
@Test
public void testInsert() {
DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
assertEquals(0, list.size());
list.insert(1);
assertEquals(1, list.size());
list.insert(2);
assertEquals(2, list.size());
list.insert(3);
assertEquals(3, list.size());
}
@Test
public void testDelete() {
DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
list.insert(1);
list.insert(2);
list.insert(3);
assertEquals(3, list.size());
list.delete(2);
assertEquals(2, list.size());
assertNull(list.find(2));
list.delete(1);
assertEquals(1, list.size());
assertNull(list.find(1));
list.delete(3);
assertEquals(0, list.size());
assertNull(list.find(3));
}
@Test
public void testFind() {
DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
list.insert(1);
list.insert(2);
list.insert(3);
assertEquals(3, list.size());
assertEquals(2, (int) list.find(2).getData());
assertNull(list.find(4));
}
@Test
public void testReverse() {
DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
list.insert(1);
list.insert(2);
list.insert(3);
assertEquals(3, list.size());
assertEquals(1, (int) list.head.getData());
assertEquals(3, (int) list.tail.getData());
list.reverse();
assertEquals(3, (int) list.head.getData());
assertEquals(1, (int) list.tail.getData());
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)