嗨,我正在努力实现一个链表,而不导入任何 Java 库。我已经实施了以下课程。我有几个测试用例来确保链接列表对象在追加和插入失败后不会更改。有人可以查看我的代码吗?我需要做什么才能使最后两个输出正确?谢谢你。
class LinkedList {
LinkedList.Node head = null;
int size = 0;
class Node {
char value;
Node next = null;
public Node(char value){
this.value = value;
this.next = null;
}
public char getValue() {
return value;
}
public void setValue(char value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public void concat(LinkedList otherList) {
if (otherList == null) {
return;
}
else if (this.head == null) {
this.head = otherList.head;
size += otherList.size;
} else {
LinkedList.Node current = this.head;
while (current.next != null) {
current = current.next;
}
current.next = otherList.head;
size += otherList.size;
}
}
public LinkedList append(char ch) {
if (head == null) {
head = new Node(ch);
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = new Node(ch);
}
size++;
return this;
}
public static String toString(LinkedList list) {
String result= "";
int i= 0;
while (i < list.length()) {
result += list.getCharAt(i);
i++;
}
return result;
}
public char getCharAt(int position) throws IndexOutOfBoundsException {
Node current = head;
for(int nodeIndex = 0; nodeIndex < size; nodeIndex++){
if(nodeIndex == position) {
break;
}
else{
current = current.next;
}
}
return current.value;
}
public int length() {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
public void insert(int position, char ch) throws IndexOutOfBoundsException {
if (position == size) {
append(ch);
} else if (position > size) {
throw new IndexOutOfBoundsException();
} else {
Node newNode = new Node(ch);
if (head == null) {
head = newNode;
} else if (position == 0) {
newNode.next = head;
head = newNode;
} else {
Node current = head;
Node previous = null;
for (int i = 0; i < position; i++) {
previous = current;
current = current.next;
}
newNode.next = current;
previous.next = newNode;
}
size++;
}
}
}
主类 -> 查看最后两个系统输出语句,问题出在哪里。
public class Main {
public static void main(String[] args) {
LinkedList list1= new LinkedList();
LinkedList list2= new LinkedList();
list1= list1.append('t').append('i').append('n').append('k').append('e').append('r');
list2= list2.append('b').append('e').append('l').append('l');
System.out.println(LinkedList.toString(list1)); // tinker correct
System.out.println(LinkedList.toString(list2)); // bell correct
list1.concat(list2); // tikerbell correct
System.out.println(LinkedList.toString(list1)); // tinkerbell correct
System.out.println(LinkedList.toString(list2)); // bell correct
list1.append('s'); // make tinkerbell -> tinkerbells
list2.insert(3, 'e'); //bell -> belel
System.out.println(LinkedList.toString(list1)); // printing tinkerbelels - should be tinkerbells - incorrect
System.out.println(LinkedList.toString(list2)); // printing belels - should be belel - incorrect
}
}
这是因为您在最后两个打印语句之前连接列表
在 concat 语句之前,您的列表看起来像这样
List 1 ==> t-> i -> n -> k -> e -> r
List 2 ==> b-> e -> l->l
连接后,
List1 ==> t-> i -> n-> k-> r-> b-> e-> l-> l
^
and List 2 ===================-|
基本上,您希望将 List2 中的内容追加到 List1 中。但 List2 引用本身连接到 List1 的末尾。因此,您在 List1 中所做的更改也将反映在 List2 参考中,反之亦然。
要修复此问题,您需要更改 concat 语句以将值附加到 otherList 而不是 otherList 本身。了解有关浅复制和深复制的更多信息。 此链接可能有用