用 java这种语言以测试驱动开发实现单链表的构建、插入、删除、查找、反转。
时间: 2023-10-19 20:03:33 浏览: 44
以下是使用 Java 进行测试驱动开发实现单链表的构建、插入、删除、查找、反转的示例代码:
```
import org.junit.Test;
import static org.junit.Assert.*;
public class LinkedListTest {
@Test
public void testLinkedList() {
LinkedList list = new LinkedList();
assertNull(list.head());
// Test insert
list.insert(1);
assertEquals(1, list.head().data());
list.insert(2);
assertEquals(2, list.head().data());
list.insert(3);
assertEquals(3, list.head().data());
// Test find
Node node = list.find(2);
assertNotNull(node);
assertEquals(2, node.data());
// Test delete
list.delete(2);
node = list.find(2);
assertNull(node);
assertEquals(3, list.head().data());
// Test reverse
list.reverse();
assertEquals(1, list.head().data());
assertEquals(3, list.head().next().next().data());
}
}
class LinkedList {
private Node head;
public Node head() {
return head;
}
public void insert(int data) {
Node newNode = new Node(data);
newNode.setNext(head);
head = newNode;
}
public Node find(int data) {
Node current = head;
while (current != null && current.data() != data) {
current = current.next();
}
return current;
}
public void delete(int data) {
if (head == null) {
return;
}
if (head.data() == data) {
head = head.next();
return;
}
Node current = head;
while (current.next() != null && current.next().data() != data) {
current = current.next();
}
if (current.next() == null) {
return;
}
current.setNext(current.next().next());
}
public void reverse() {
Node previous = null;
Node current = head;
while (current != null) {
Node next = current.next();
current.setNext(previous);
previous = current;
current = next;
}
head = previous;
}
}
class Node {
private int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
public int data() {
return data;
}
public Node next() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
```
在上述代码中,我们首先编写了一个 `LinkedListTest` 类,用于测试 `LinkedList` 类的各个方法。在测试方法中,我们首先创建了一个空链表,并确保其头节点为 `null`。接着,我们测试了插入、查找、删除和反转等方法,确保其能够按预期工作。
在 `LinkedList` 类中,我们定义了一个 `head` 变量,用于跟踪链表的头节点。在 `insert` 方法中,我们创建了一个新节点,并将其插入到链表的头部。在 `find` 方法中,我们遍历链表,查找指定数据的节点。在 `delete` 方法中,我们遍历链表,查找指定数据的节点,并删除它。在 `reverse` 方法中,我们使用迭代方式反转链表。
最后,我们还定义了一个 `Node` 类,用于表示链表中的节点。每个节点包含一个整型数据和一个指向下一个节点的指针。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)