Recursion and backward recursion loop via hasNext () method

Hi everyone I'm new to java so I really appreciate any help on this. So, here is the problem I ran into: I have a list class and a listNode class. The List class is represented by name, firstNode and lastNode. firstNode and lastNode are of type listNode, listNode is represented by an object (for ex.data or Object o) and nextNode, which points to the next Node in the list, which is also of type listNode.

List class:

public class List {

private ListNode firstNode;
private ListNode lastNode;
private String name;

public List() {
    this("list");
}

public List(String listName) {
    name = listName;
    firstNode = lastNode = null;
}

public void insertAtFront(Object insertItem) {
    if (isEmpty())
        firstNode = lastNode = new ListNode(insertItem);
    else
        firstNode = new ListNode(insertItem, firstNode);
}

public void insertAtBack(Object insertItem) {
    if (isEmpty())
        firstNode = lastNode = new ListNode(insertItem);
    else
        lastNode = lastNode.nextNode = new ListNode(insertItem);
}

public Object removeFromFront() throws EmptyListException {
    if (isEmpty())
        throw new EmptyListException(name);
    Object removedItem = firstNode.data;

    if (firstNode == lastNode)
        firstNode = lastNode = null;
    else
        firstNode = firstNode.nextNode;
    return removedItem;
}

public Object removeFromBack() throws EmptyListException {
    if (isEmpty())
        throw new EmptyListException(name);

    Object removedItem = lastNode.data;
    if (firstNode == lastNode)
        firstNode = lastNode = null;
    else {
        ListNode current = firstNode;

        while (current.nextNode != lastNode)
            current = current.nextNode;

        lastNode = current;
        current.nextNode = null;
    }
    return removedItem;
}

public boolean isEmpty() {
    return firstNode == null;
}

public void print() {
    if (isEmpty()) {
        System.out.printf("Empty %s\n", name);
        return;
    }
    System.out.printf("The %s is : ", name);
    ListNode current = firstNode;

    while (current != null) {
        System.out.printf("%s", current.data);
        current = current.nextNode;
    }
    System.out.println("\n");
}

@Override
public String toString() {
    String stk = "(";
    if(isEmpty())return "Empty List";
    ListNode checkNode = firstNode;
        while (checkNode != null) {
        stk += checkNode.data.toString()+ " , ";
        checkNode = checkNode.nextNode;
    }
    return stk+")";
}
public ListNode removeAt (int k){
    if(k<=0 || k>getLength())
        try{
            throw new IllegalValues();
        }catch(IllegalValues iv){
            iv.printStackTrace();
            return null;
        }
    ListNode newNode = firstNode;
    if (k==1) {
        ListNode removedNode = firstNode;
        firstNode = firstNode.nextNode;
        return removedNode;
    }
    ListNode someNode = firstNode;
    for (int i = 1; i < k - 1; i++) {
        someNode = someNode.nextNode;
    }
    ListNode removedNode = someNode.nextNode;
    someNode.nextNode = someNode.nextNode.nextNode;
    return removedNode;
}
public int getLength(){
    ListNode checkNode = firstNode;
    int count =0;
    while (checkNode != null) {
    count++;
    checkNode = checkNode.nextNode;
}
    return count;
}
public  void show(){
    if (firstNode==null)
      return;
    else
        System.out.print(firstNode + " ,");
      firstNode.show();
    }
public  void showRev(){
    if (lastNode==null)
      return;
    else
        System.out.println(lastNode + ",");
      lastNode.showRev();
    }
    }   

      

ListNode class

public class ListNode {

Object data;
ListNode nextNode;

public ListNode(Object o) {
    this(o, null);
}

public ListNode(Object o, ListNode node) {
    data = o;
    nextNode = node;
}

public Object getObject() {
    return data;
}

public ListNode getNext(){
    return nextNode;
}

public ListNode show() {
if(this.nextNode == null)return this;
ListNode displayMe = nextNode.show();
System.out.print(displayMe + " , ");
return displayMe;

}

public ListNode showRev() {
    if(this.firstNode == null)return this;
    ListNode displayMe = lastNode.show();
    System.out.print(displayMe + " , ");
    return displayMe;

}

}

      

I have a recursive method called show that displays all objects in a list from start to finish. Now I'm trying to do something like this (the method name is showRev ()) that displays objects from end to beginning (recursive method) and I don't think the previous method can be done, so I'm kind of stuck with this method. Eid really appreciate any ideas Thanks guys

+3


source to share


1 answer


If your method is showRev

allowed to take an argument, we can store each ListNode

in java.util.List

:

public java.util.List<ListNode> showRev(java.util.List<ListNode> nodes) {
    if (this.nextNode == null) {
        Collections.reverse(nodes);

        System.out.println(nodes.stream().collect(Collectors.joining(" ")));

        return nodes;
    }

    nodes.add(lastNode.show());

    return showRev(nodes);
}

      

Note that recursion does nothing special here, but adds ListNode

to java.util.List

.



To call this method, just pass it new ArrayList<>()

.

Also, I would refrain from using it List

as a name for the class, since it java.util.List

can be easily counted with.

+2


source







All Articles