对于此作业,我需要以链接列表作为参数而不是节点以递归方式反向打印链接列表。如何以递归方式通过链接列表引用作为参数传递链接列表[作业]
我也有使用,我的教授提供的这个SinglyLinkedList类:
public class SinglyLinkedList<E> {
private int length; // # elements in the linked list
private SLNode<E> head; // access point to the linked list
private SLNode<E> tail;
public SinglyLinkedList() {
this.length = 0;
this.tail = new SLNode<E>(); // the tail dummy node
this.head = new SLNode<E> (null, this.tail); // the head dummy node
}
public int getLength() {
return this.length;
}
public void add(E e) {
SLNode<E> newnode = new SLNode<E> (e, null);
newnode.setSuccessor(this.head.getSuccessor());
this.head.setSuccessor(newnode);
this.length++;
}
public void add(E e, int p) {
// verify that index p is valid
if ((p < 0) || (p > this.length)) {
throw new IndexOutOfBoundsException("index " + p
+ " is out of range: 0 to " +
this.length);
}
SLNode<E> newnode = new SLNode<E> (e, null);
SLNode<E> cursor = this.head;
for (int i = 0; i < p; i++) {
cursor = cursor.getSuccessor();
}
addAfter(cursor, newnode);
this.length++;
}
public E remove(int p) {
if ((p < 0) || (p >= this.length)) {
throw new IndexOutOfBoundsException("index " + p
+ " is out of range: 0 to " +
(this.length - 1));
}
SLNode<E> cursor = head; // good for p == 0
if (p > 0) {
cursor = find(p - 1); // get target's predecessor
}
SLNode<E> target = cursor.getSuccessor(); // get the node to remove
// link target to cursor's successor
cursor.setSuccessor(target.getSuccessor());
target.setSuccessor(null);
cursor.setElement(null);
this.length--;
return target.getElement();
}
public E getElementAt(int p) {
SLNode<E> node = this.find(p);
return node.getElement();
}
private void addAfter(SLNode<E> p, SLNode<E> newnode) {
newnode.setSuccessor(p.getSuccessor());
p.setSuccessor(newnode);
}
private SLNode<E> find(E target) {
SLNode<E> cursor = head.getSuccessor();
while (cursor != tail) {
if (cursor.getElement().equals(target)) {
return cursor; // success
}
else {
cursor = cursor.getSuccessor();
}
}
return null; // failure
}
private SLNode<E> find(int p) {
if ((p < 0) || (p >= this.length)) {
throw new IndexOutOfBoundsException();
}
SLNode<E> cursor = head.getSuccessor();
int i = 0;
while (i != p) {
cursor = cursor.getSuccessor();
i++;
}
return cursor;
}
}
我无法弄清楚如何编写方法与参考传递到一个单向链表,而不是一个节点。先谢谢您的帮助!
想一想 - 如果您在列表中随意走动列表并打印,则会按照“前进”顺序打印列表。如果您将清单走到最后,然后向后追溯您的步骤,在每个后退步骤打印,您将按照“反向”顺序打印清单。你可以用递归调用“走”列表,然后返回是一个“退步”。 –
尽管我没有亲眼看到没有“走路节点”的方法,但只使用上述公共方法。似乎唯一的方法就是走这个列表(通过使用elementAt或者在走路时删除头节点),并在走路时建立一个新列表,然后打印新列表。 –
打印期间可以修改(销毁)列表吗?还是应该保留? –