I'm working on a project that can recursively print out the items from a single link list. The project also require the reverse method should accept a reference to a generic single-linked list. This is my logic for printing the item in reverse:
if the element in a node is not equal to null, then we don't print anything till the next element become a null. When the next element equal to null, then we start to print the item.
1-->2-->3-->4-->null
start to print from:
1--<2--3<--4<--null
But I got stuck implement the methods. This is how my method header look like:
public <E> SinglyLinkedList reverse(SinglyLinkedList<E> list)
{
}
This is my SinglyLinkedList class:
public class SinglyLinkedList<E> {
private int length; // # elements in the linked list
private SLNode<E> head; // access point to the linked list
private SLNode<E> tail;
public SinglyLinkedList() {
this.length = 0;
this.tail = new SLNode<E> (); // the tail dummy node
this.head = new SLNode<E> ( null, this.tail ); // the head dummy node
}
public int getLength() {
return this.length;
}
void add( E e ) {
SLNode<E> newnode = new SLNode<E> ( e, null );
newnode.setSuccessor( this.head.getSuccessor() );
this.head.setSuccessor( newnode );
this.length++;
}
public void add( E e, int p ) {
// verify that index p is valid
if ( ( p < 0 ) || ( p > this.length ) ) {
throw new IndexOutOfBoundsException( "index " + p
+ " is out of range: 0 to " +
this.length );
}
SLNode<E> newnode = new SLNode<E> ( e, null );
SLNode<E> cursor = this.head;
for ( int i = 0; i < p; i++ ) {
cursor = cursor.getSuccessor();
}
addAfter( cursor, newnode );
this.length++;
}
public E remove( int p ) {
if ( ( p < 0 ) || ( p >= this.length ) ) {
throw new IndexOutOfBoundsException( "index " + p
+ " is out of range: 0 to " +
( this.length - 1 ) );
}
SLNode<E> cursor = head; // good for p == 0
if ( p > 0 ) {
cursor = find( p - 1 ); // get target's predecessor
}
SLNode<E> target = cursor.getSuccessor(); // get the node to remove
// link target to cursor's successor
cursor.setSuccessor( target.getSuccessor() );
target.setSuccessor( null );
cursor.setElement( null );
this.length--;
return target.getElement();
}
public E getElementAt( int p ) {
SLNode<E> node = this.find( p );
return node.getElement();
}
private void addAfter( SLNode<E> p, SLNode<E> newnode ) {
newnode.setSuccessor( p.getSuccessor() );
p.setSuccessor( newnode );
}
private SLNode<E> find( E target ) {
SLNode<E> cursor = head.getSuccessor();
while ( cursor != tail ) {
if ( cursor.getElement().equals( target ) ) {
return cursor; // success
}
else {
cursor = cursor.getSuccessor();
}
}
return null; // failure
}
private SLNode<E> find( int p ) {
if ( ( p < 0 ) || ( p >= this.length ) ) {
throw new IndexOutOfBoundsException();
}
SLNode<E> cursor = head.getSuccessor();
int i = 0;
while ( i != p ) {
cursor = cursor.getSuccessor();
i++;
}
return cursor;
}
@Override
public String toString()
{
SLNode current = head.getSuccessor();
String output = " ";
while(current != null)
{
output += "|" + current.getElement().toString() + "|";
current = current.getSuccessor();
}
return output;
}
}
This is my node class:
public class SLNode<E> {
private E element; // the data field
private SLNode<E> successor; // the link to this node's successor
public SLNode() {
this.element = null;
this.successor = null;
}
public SLNode( E theElement, SLNode<E> theSuccessor ) {
this.element = theElement;
this.successor = theSuccessor;
}
public E getElement() {
return this.element;
}
public void setElement( E newElement ) {
this.element = newElement;
}
public SLNode<E> getSuccessor() {
return this.successor;
}
public void setSuccessor( SLNode<E> newSuccessor ) {
this.successor = newSuccessor;
}
}
In my parameter I need to take SinglyLinkedList<E> list, but there are no method inside the SinglyLinkedList class would allow me to travel the element. How to implement the reverse method?