java - 如何从 Java 中的 LinkedList 中删除元素?如何将第一个元素设置为 Java 中链表的头部?
问题描述
我正在学习链接列表,我对最初创建 LinkedList 时创建的 Node 有点困惑。所以我使用一个空的构造函数来创建列表,但它也创建了根节点并将“next”设置为“null”,但是“int data”的初始值总是“0”,我不想要那个(如果我打印详细信息,它会打印 0)。
参见代码下方的打印,新元素出现在列表中后,初始的 0 仍然存在。事实上,它继续存在,不应该是这样。
另一件事,我的删除功能不起作用,是什么问题?
public class LinkedList {
Node root;//the beginning - root element of type Node(check inner class below
int size;//keeps track of the size of the list
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
System.out.println(linkedList.getSize());
System.out.println("Root: " + linkedList.root.getData());
linkedList.prepend(234);
linkedList.prepend(45);
linkedList.prepend(33);
linkedList.prepend(222);
System.out.println("Root: " + linkedList.root);
System.out.println(linkedList.printDetails());
System.out.println(linkedList.getSize());
System.out.println("Root: " + linkedList.root.getData());
linkedList.remove(222);
System.out.println("Root: " + linkedList.root.getData());
linkedList.find(33).setData(34);
System.out.println(linkedList.printDetails());
System.out.println("Root: " + linkedList.root.getData());
linkedList.append(6565);
linkedList.append(144);
System.out.println("Root: " + linkedList.root.getData());
System.out.println(linkedList.getSize());
System.out.println(linkedList.printDetails());
}
public LinkedList() {
root = new Node();
size = 0;
}
public int getSize() {
System.out.println("Size: " + size);
return size;
}
public void setSize(int size) {
this.size = size;
}
public StringBuilder printDetails() {
System.out.print("Linked List: ");
StringBuilder details = new StringBuilder("[");
Node currentNode = this.root;
while (currentNode != null) {
details.append(currentNode.getData()).append(",");
currentNode = currentNode.getNext();
}
details.deleteCharAt(details.length() - 1);
details.append("]");
return details;
}
public void prepend(int data) {
this.root = new Node(data, root);
this.size++;
}
public void append(int data) {
Node currentNode = this.root;
Node newNode = new Node(data);
while (currentNode.getNext() != null) {
currentNode = currentNode.getNext();
}
currentNode.setNext(newNode);
this.size++;
}
public Node find(int data) {
Node currentNode = this.root;
while (currentNode != null) {
if (currentNode.getData() == data)
return currentNode;
currentNode = currentNode.getNext();
}
return null;
}
public void remove(int data) {
Node currentNode = this.root;
Node previousNode = new Node();
while (currentNode != null) {
if (currentNode.getData() == data) {
previousNode.setNext(currentNode.getNext());
this.setSize(this.getSize() - 1);
return;
} else {
previousNode = currentNode;
currentNode = currentNode.getNext();
}
}
}
private static class Node {
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
try {
return next;
} catch (NullPointerException e) {
System.out.println((char[]) null);
}
return null;
}
public void setNext(Node next) {
this.next = next;
}
private Node() {
}
private Node(int data, Node next) {
this.data = data;
this.next = next;
}
private Node(int data) {
this.data = data;
}
}
}
印刷:
Size: 0
0
Root: 0
Root: LinkedList$Node@548c4f57
Linked List: [222,33,45,234,0]
Size: 4
4
Root: 222
Size: 4
Root: 222
Linked List: [222,34,45,234,0]
Root: 222
Root: 222
Size: 5
5
Linked List: [222,34,45,234,0,6565,144]
解决方案
您的 remove 方法的实现没有考虑到该root
元素可能是您不想删除的元素。将此附加到您的删除方法的头部
if (this.root.getData() == data) {
this.root = this.root.getNext() != null
? this.root.getNext()
: new Node();
}
关于总是用 0 初始化的事实。data
我不确定我是否理解这个问题,你不希望它是什么?
推荐阅读
- oracle - 有没有办法在 Netsuite 保存的搜索中排列具有相同值的行(将它们分组)在其他行下方?
- vba - VBA查找具有匹配功能的列号
- flutter - 如何禁用 TextFormField 默认动画
- scala - 火花数据框收集特定结果
- swift - Swift 从 Firestore 数据库错误时间戳中获取数据
- bash - 将相对路径更新为 .html 文件中的“部分”绝对路径
- reactjs - 在服务器端渲染中更新 React Context
- c++ - 消费者生产者多线程冻结
- contract - pancakeswap 的农场合同
- spring-boot - JUnit @DisplayName 在詹金斯报告中不起作用