문제

I am trying to print out the elements of a Linked List in Java in reverse using recursion, but I don't know exactly how to go about that. I've tried looking at several examples but it doesn't look like there is anything that tries to accomplish what I need to do. I did it with a for loop but I know that completely defeats the purpose of this assignment. This is what I have so far:

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
}

I apologize if it's kinda sloppy, It's obviously not finalized in any way, but I am unsure of how to go about turning the reverse method into something that does the same thing but with recursion. Please help!

EDIT: If i need to have a seperate node class for this (not sure if I do or not), I do have this SLNode class that was provided a few weeks ago that I'm assuming would work:

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;
}
}

But once again...I'm not sure how to go about implementing it all..

도움이 되었습니까?

해결책 2

Here's how I did it:

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);
    }
}

Basically, I create a copy of the given list, print out it's last value, remove that value, and then repeat with the new list if there is anything left in the list.

다른 팁

Eliminating from the begining of the list and passing the rest to the recursion:

    public void reverse (LinkedList<E> a) {
        if (a.size()>0) {
            E current = a.removeFirst();
            reverse(a);
            System.out.println(current);            
        }
    }

This is my solution

// 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);

    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top