使用java实现链表的代码封装,要求包括提取元素,插入元素,删除元素,寻找元素,写到一个文件里面
时间: 2023-05-27 13:07:18 浏览: 40
链表的节点类:
```java
public class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
this.next = null;
}
public void setNext(Node<T> next) {
this.next = next;
}
public Node<T> getNext() {
return next;
}
public T getData() {
return data;
}
}
```
链表类:
```java
public class LinkedList<T> {
private Node<T> head;
private int size;
public LinkedList() {
head = null;
size = 0;
}
public void insert(T data) {
Node<T> newNode = new Node<>(data);
if (head == null) {
head = newNode;
} else {
newNode.setNext(head);
head = newNode;
}
size++;
}
public boolean delete(T data) {
if (head == null) {
return false;
} else if (head.getData().equals(data)) {
head = head.getNext();
size--;
return true;
} else {
Node<T> currentNode = head;
while (currentNode.getNext() != null) {
if (currentNode.getNext().getData().equals(data)) {
currentNode.setNext(currentNode.getNext().getNext());
size--;
return true;
}
currentNode = currentNode.getNext();
}
return false;
}
}
public boolean contains(T data) {
Node<T> currentNode = head;
while (currentNode != null) {
if (currentNode.getData().equals(data)) {
return true;
}
currentNode = currentNode.getNext();
}
return false;
}
public T get(int index) {
if (index < 0 || index >= size) {
return null;
} else {
Node<T> currentNode = head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.getNext();
}
return currentNode.getData();
}
}
public int size() {
return size;
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.insert(1);
list.insert(2);
list.insert(3);
System.out.println(list.size()); // 3
System.out.println(list.contains(2)); // true
System.out.println(list.contains(4)); // false
System.out.println(list.get(1)); // 2
list.delete(2);
System.out.println(list.size()); // 2
System.out.println(list.get(1)); // 1
}
}
```
代码已经封装成一个文件了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)