我正在尝试使用递归打印出Java中链接列表的元素,但我不确切知道如何去做。我试过看几个例子,但看起来没有任何东西试图完成我需要做的事情。我用for循环做了,但我知道这完全违背了这个任务的目的。这就是我到目前为止所做的:
public class Recursion <E>{
public void add (E list, Object element) {
((LinkedList) list).add(element);
}// end add
public void reverse (LinkedList<E> a) {
for (int i = a.size() - 1; i >= 0; i--) {
System.out.println(a.get(i));
}
}//end reverse
public static void main(String[] args) {
Recursion test = new Recursion();
LinkedList<Object> list = new LinkedList<>();
test.add(list, 1);
test.add(list, 2);
test.add(list, 3);
test.add(list, 4);
System.out.println(list);
test.reverse(list);
}//end main
}
我道歉,如果它有点草率,它显然没有以任何方式最终确定,但我不确定如何将reverse
方法转变为做同样事情的事情但随着递归。请帮忙!
编辑:如果我需要有一个单独的节点类(不确定我是否这样做),我确实有几周前提供的SLNode类,我假设它可以工作:< / p>
public class SLNode <E>
{
public E element;
public SLNode next;
public SLNode()
{
this.element = null;
this.next = null;
}
public SLNode(E element, SLNode<E> node)
{
this.element = element;
this.next = node;
}
public E getElement()
{
return this.element;
}
public void setElement(E element)
{
this.element = element;
}
public SLNode<E> getSuccessor()
{
return this.next;
}
public void setSuccessor(SLNode next)
{
this.next = next;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Element: " + element;
}
}
但又一次......我不确定如何实施这一切......
答案 0 :(得分:2)
从列表的开头消除并将其余部分传递给递归:
public void reverse (LinkedList<E> a) {
if (a.size()>0) {
E current = a.removeFirst();
reverse(a);
System.out.println(current);
}
}
答案 1 :(得分:1)
以下是我的表现:
public void reverse(LinkedList<E> a) {
LinkedList<E> list = new LinkedList<E>(a);
System.out.println(list.getLast());
list.removeLast();
if(list.size() > 0) {
reverse(list);
}
}
基本上,我创建了给定列表的副本,打印出它的最后一个值,删除该值,然后如果列表中还剩下任何内容则重复新列表。
答案 2 :(得分:0)
这是我的解决方案
// 1. recursiveDisplayElements --> Displays all elements of a MyDynamicList
/**
* Given a concrete MyDynamicList, this recursive algorithm displays its
* elements by screen (if any).
*
* @param m:
* The MyDynamicList we want to display its elements.
*/
public void recursiveDisplayElements(MyDynamicList m) {
// -----------------------------
// SET OF OPS
// -----------------------------
// -----------------------------
// I. SCENARIO IDENTIFICATION
// -----------------------------
int scenario = 0;
// Rule 1. MyDynamicList is empty
if (m.length() == 0)
scenario = 1;
// Rule 2. MyDynamicList is non-empty
else
scenario = 2;
// We create size- holds index of last element of MyDanamicList
int size = m.length();
// -----------------------------
// II. SCENARIO IMPLEMENTATION
// -----------------------------
switch (scenario) {
// Rule 1. MyDynamicList is empty
case 1:
System.out.println();
break;
case 2:
// 1. We get the last element of MyDynamicList
int e = m.getElement(size - 1);
// 2. We print the last element from MyDanamic list
System.out.println(e);
// 3. We remove the last element from MyDynamicList we just checked
m.removeElement(size - 1);
// 4. We recursively solve the smaller problem
recursiveDisplayElements(m);
// 5. We also add the element back to m, so as to not to modify its original
// state
m.addElement(size - 1, e);
}
}