我正在学习链表,我对最初创建链表时创建的节点有点困惑。所以我使用一个空的构造函数只是为了创建列表,但它也会创建根节点并将"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();
}
关于data
总是用 0 初始化的事实。我不确定我是否理解这个问题,你不想是什么?
在您的删除方法中,有些情况您没有考虑到,我已经在代码的注释中进行了解释。这是第一种方法
public void remove(int data) {
Node currentNode = this.root;
Node previousNode = null;
while (currentNode != null && currentNode.data != data) {
//Here we are searching if the element exists on the list
previousNode = currentNode;
currentNode = currentNode.next;
}
if (currentNode == null) //If we enter here, the element doesn't exist
{
System.out.println("Value not found");
} else {
int value = currentNode.data;
if (previousNode == null) { //Check if the element is the first one in our list
this.root = currentNode.next;
this.setSize(this.getSize() - 1);
return;
}
if (currentNode.next != null) { //Check if the element is the last one in our list
previousNode.next = currentNode.next;
System.out.println("Value " + value + " found");
} else {
previousNode.next = null;
System.out.println("Value " + value + " found");
}
this.setSize(this.getSize() - 1);
}
}
PS:你应该跟踪两端