I am currently trying print a doubly linked list in reverse order recursively, but can't seem to make it work. I have even emailed my prof and followed his directions and still nothing works.. can anyone tell me what i am doing wrong?
import java.awt.List;
public class DoubleLinkedList<T>
{
DoubleLinkedList(){}
public class Node<T>
{
public Node next;//next node
public Node prev;//previous node
public T data; //info contained
public Node(T data)
{
this.data=data;// constructor for node
}
public void printNode()
{
System.out.print(this.data + " ");
}
@Override
public String toString()
{
return data.toString();
}
}
public Node first=null;
public Node last= null;
public void insert (T data, int position) // insert item
{
Node newNode = new Node(data);
if (isEmpty())
{
newNode.next = null;
newNode.prev = null;
first = newNode;
last = newNode;
}
else
{
Node iterator = first;
for(int i = 1; i<position;i++)
{
iterator = iterator.next;
}
iterator.prev.next = newNode;
iterator.prev = newNode;
newNode.prev = iterator.prev.prev;
newNode.next = iterator;
}
}
public boolean isEmpty() { return first == null;}
public void delete() // delete item
{
if (first.next == null)
{
first = null; // checks for list
last = null;
}
else
{
last = last.prev;
last.next = null;
}
}
public void add (T data) // add item
{
Node newNode= new Node(data);
if (isEmpty())
{
newNode.next = null;
newNode.prev = null;
first = newNode;
last = newNode;
}
else
{
last.next = newNode;
newNode.prev = last;
newNode.next = null;
last = newNode;
}
}
public void printList()
{
Node iterator = first;
System.out.print("List: ");
while(iterator != null)
{
iterator.printNode();
iterator = iterator.next;
}
System.out.println("");
}
public void printListReverse() // prints recursively
{
Node iterator = last;
System.out.println("Reversed List: "+ last); // we'll print out our node now
if(iterator.prev != null) // we recurse every time unless we're on the last one
{
iterator = iterator.prev;
printListReverse(); // this says "do this to the next node"
System.out.print("Reversed order: ");
}
System.out.println();
}
}
TV_Show. java
public class TV_Show {
public static void main(String args[]) {
DoubleLinkedList list = new DoubleLinkedList();
list.add("Qauntico");
list.add("The Flash");
list.add("Supergirl");
list.add("Big Bang Theory");
list.add("Arrow ");
list.add("NCIS ");
list.printList();
list.insert("Agent Carter",3);
list.insert("Luke Cage",5);
list.printList();
list.delete();
list.delete();
list.delete();
list.printList();
list.printListReverse();
}
}
import java.awt.List;
public class DoubleLinkedList<T>
{
DoubleLinkedList(){}
public class Node<T>
{
public Node next;//next node
public Node prev;//previous node
public T data; //info contained
public Node(T data)
{
this.data=data;// constructor for node
}
public void printNode()
{
System.out.print(this.data + " ");
}
@Override
public String toString()
{
return data.toString();
}
}
public Node first=null;
public Node last= null;
public void insert (T data, int position) // insert item
{
Node newNode = new Node(data);
if (isEmpty())
{
newNode.next = null;
newNode.prev = null;
first = newNode;
last = newNode;
}
else
{
Node iterator = first;
for(int i = 1; i<position;i++)
{
iterator = iterator.next;
}
iterator.prev.next = newNode;
iterator.prev = newNode;
newNode.prev = iterator.prev.prev;
newNode.next = iterator;
}
}
public boolean isEmpty() { return first == null;}
public void delete() // delete item
{
if (first.next == null)
{
first = null; // checks for list
last = null;
}
else
{
last = last.prev;
last.next = null;
}
}
public void add (T data) // add item
{
Node newNode= new Node(data);
if (isEmpty())
{
newNode.next = null;
newNode.prev = null;
first = newNode;
last = newNode;
}
else
{
last.next = newNode;
newNode.prev = last;
newNode.next = null;
last = newNode;
}
}
public void printList()
{
Node iterator = first;
System.out.print("List: ");
while(iterator != null)
{
iterator.printNode();
iterator = iterator.next;
}
System.out.println("");
}
public void printListReverse() // prints recursively
{
Node iterator = last;
System.out.println("Reversed List: "+ last); // we'll print out our node now
if(iterator.prev != null) // we recurse every time unless we're on the last one
{
iterator = iterator.prev;
printListReverse(); // this says "do this to the next node"
System.out.print("Reversed order: ");
}
System.out.println();
}
}
TV_Show. java
public class TV_Show {
public static void main(String args[]) {
DoubleLinkedList list = new DoubleLinkedList();
list.add("Qauntico");
list.add("The Flash");
list.add("Supergirl");
list.add("Big Bang Theory");
list.add("Arrow ");
list.add("NCIS ");
list.printList();
list.insert("Agent Carter",3);
list.insert("Luke Cage",5);
list.printList();
list.delete();
list.delete();
list.delete();
list.printList();
list.printListReverse();
}
}