Recursion and Reverse Recursion looping through hasNext() method

Viewed 195

Hi everyone I'm new to java so Id really appreciate any help on this. Ok so Here is The Problem that I have encountered: I have a list class and a listNode class, The list Class is represented by a name, a firstNode and a lastNode. firstNode and lastNode are from type listNode, a listNode is represented by an Object (for ex. data or Object o) and a nextNode which points to the next Node in the list which is also from 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 which displays all the objects in the list from the beginning to the end now I'm trying to make something similar (the method name is showRev() ) which displays the objects from the end to the beginning ( a recursive method) and I dont think it is possible to make a has previous method so im kinda stuck on this method. Id really appreciate any Ideas Thanks guys

1 Answers
Related