因此,我有一个使用提供的节点类的自定义linkedList(单个)类。
现在我有另一个类来QuickSort这个。(为了简洁起见,我只想专注于编辑L.head)
但是,我不想将quicksort方法传递给整个linkedList,而是将其从链表中传递给头部。
我的问题是访问此头节点并从quckSort方法/类更改它,我不想删除它或只是更改元素值
主:
public class TestLinkedList {
public static <E extends Comparable<E>> void main(String[] args) {
MyLinkedList<Integer> L = new MyLinkedList<Integer>();
L.add(3);
L.add(1);
L.add(2);
System.out.println("Initial=" + L);
MySort.quickSort(L.head);
System.out.println("After ="+L);
}
}
快速排序:
public class MySort {
public static <E extends Comparable<E>> void quickSort(MyNode<E> list) {
list = list.next;
}
节点类:
public class MyNode<E extends Comparable<E>> {
E element;
MyNode<E> next;
public MyNode(E item) {
element = item;
next = null;
}
}
LinkedList类:
public class MyLinkedList<E extends Comparable<E>> {
public MyNode<E> head;
/**
* Default constructor
*/
public MyLinkedList() {
this.head = new MyNode<E>(null);
}
/**
* Adds given element to MylinkedList
*
* @param e
* Given element to be added
*/
public void add(E e) {
assert e != null : "Violation of: e is not null";
MyNode<E> temp = new MyNode<E>(e);
if (head.element != null) {
temp.next = head;
head = temp;
} else {
this.head = temp;
}
}
/**
* Given a certain element, will try to locate and return whether it exists
* in the MylinkedList
*
* @param e
* Element being searched for
* @return Result Result of whether this MyLinkedList contains the given
* Element
*/
public boolean find(E e) {
assert e != null : "Violation of: e is not null";
boolean result = false;
MyNode<E> temp = head;
outerloop: while (temp.element != null && temp.next != null) {
if (temp.element.equals(e)) {
result = true;
break outerloop;
}
temp = temp.next;
}
return result;
}
/**
* Inserts given element before specific element
*
* @param e
* Element being inserted before
* @param e2
* Element being inserted
*/
public void insertElementBefore(E e1, E e2) {
assert e1 != null : "Violation of: e1 is not null";
assert e2 != null : "Violation of: e2 is not null";
MyNode<E> temp = head;
MyNode<E> prev = head;
MyNode<E> newElement = new MyNode<E>(e2);
outerloop: while (temp.element != null && temp.next != null) {
if (temp.element.equals(e1)) {
prev.next = newElement;
newElement.next = temp;
break outerloop;
}
prev = temp;
temp = temp.next;
}
}
/**
* Deletes given element from MylinkedList
*
* @param e
* Element being deleted
*/
public void delete(E e) {
assert e != null : "Violation of: e is not null";
MyNode<E> temp = head;
MyNode<E> prev = head;
loop: while (temp.element != null) {
if (temp.element.equals(e)) {
if (temp == head) {
head = temp.next;
} else {
prev.next = temp.next;
}
break loop;
}
prev = temp;
temp = temp.next;
}
}
/**
* Overridden toString; prints out all elements cleanly
*/
public String toString() {
String result = "\"";
MyNode<E> temp = head;
loop: while (temp.element != null) {
result += temp.element + " ";
if (temp.next != null) {
temp = temp.next;
} else {
break loop;
}
}
return result + "\"";
}
}
答案 0 :(得分:0)
在java中,参数按值传递。也就是说,您无法通过在本地方法中更改引用的地址来修改引用的地址。我建议你做的是让QuickSort()方法返回下一个Node地址。