uniquity
06-02-2009, 05:57 AM
For my 102 class, I was instructed to write two inner-classes for a linked list...one of the inner-classes (EmptyNode) is to be written without recursion (shouldn't be too difficult) and the other inner-class (ListNode) is to be written using recursion in every method. I honestly don't have any idea to start and am just looking for some suggestions and things to think about and maybe some basic coding tips for this assignment. I've coded part of the given code below. If there are additional information about this assignment you guys need, let me know. Thanks in advance :thumbsup:
private interface Node<E>
{
boolean isEmpty();
int size();
Node<E> add(int index, E element);
E get(int index);
E set (int index, E element);
int indexOf(E element);
Node<E> remove (int index);
}
/////////////////////////////////////////////////////////////////////////////
// TODO: Implement this class as follows:
//
// * With no instance variables
// * With no explicit constructors.
// * Only the methods of the Node<E> interface.
// * Don't forget the required initialization of stackTrace as the first
// line of each method (see assignment for details).
//
private class EmptyNode<E> implements Node<E>
{
// Your code here...
public boolean isEmpty()
{
stackTrace = new Throwable();
return true;
}
public int size()
{
stackTrace = new Throwable();
return 0;
}
public Node<E> add( int index, E element )
{
stackTrace = new Throwable();
//create an instance of ListNode and return it.
}
public E get( int index )
{
stackTrace = new Throwable();
return null;
}
public E set( int index, E element )
{
stackTrace = new Throwable();
return null;
}
public int indexOf( E element )
{
stackTrace = new Throwable();
return 0;
}
public Node<E> remove( int index )
{
stackTrace = new Throwable();
return null;
}
}
/////////////////////////////////////////////////////////////////////////////
// TODO: Implement this class as follows:
//
// * With only the instance variables already specified.
// * With no explicit constructors.
// * Only the methods of the Node<E> interface.
// * Don't forget the required initialization of stackTrace as the first
// line of each method (see assignment for details).
//
private class ListNode<E> implements Node<E>
{
public E element;
public Node<E> next;
// Your code here...
public ListNode()
{
}
public ListNode( E element )
{
this.element = element;
}
public boolean isEmpty()
{
stackTrace = new Throwable();
return false;
}
public int size()
{
stackTrace = new Throwable();
return 1 + next.size();
}
public Node<E> add( int index, E element )
{
stackTrace = new Throwable();
}
public E get( int index )
{
stackTrace = new Throwable();
if( index < 0 || index >= this.size() )
{
throw new IndexOutOfBoundsException();
}
return next.get( index );
}
public E set( int index, E element )
{
stackTrace = new Throwable();
return next.set( index, element );
}
public int indexOf( E element )
{
stackTrace = new Throwable();
return next.indexOf( element );
}
public Node<E> remove( int index )
{
stackTrace = new Throwable();
return next.remove( index );
}
}
private interface Node<E>
{
boolean isEmpty();
int size();
Node<E> add(int index, E element);
E get(int index);
E set (int index, E element);
int indexOf(E element);
Node<E> remove (int index);
}
/////////////////////////////////////////////////////////////////////////////
// TODO: Implement this class as follows:
//
// * With no instance variables
// * With no explicit constructors.
// * Only the methods of the Node<E> interface.
// * Don't forget the required initialization of stackTrace as the first
// line of each method (see assignment for details).
//
private class EmptyNode<E> implements Node<E>
{
// Your code here...
public boolean isEmpty()
{
stackTrace = new Throwable();
return true;
}
public int size()
{
stackTrace = new Throwable();
return 0;
}
public Node<E> add( int index, E element )
{
stackTrace = new Throwable();
//create an instance of ListNode and return it.
}
public E get( int index )
{
stackTrace = new Throwable();
return null;
}
public E set( int index, E element )
{
stackTrace = new Throwable();
return null;
}
public int indexOf( E element )
{
stackTrace = new Throwable();
return 0;
}
public Node<E> remove( int index )
{
stackTrace = new Throwable();
return null;
}
}
/////////////////////////////////////////////////////////////////////////////
// TODO: Implement this class as follows:
//
// * With only the instance variables already specified.
// * With no explicit constructors.
// * Only the methods of the Node<E> interface.
// * Don't forget the required initialization of stackTrace as the first
// line of each method (see assignment for details).
//
private class ListNode<E> implements Node<E>
{
public E element;
public Node<E> next;
// Your code here...
public ListNode()
{
}
public ListNode( E element )
{
this.element = element;
}
public boolean isEmpty()
{
stackTrace = new Throwable();
return false;
}
public int size()
{
stackTrace = new Throwable();
return 1 + next.size();
}
public Node<E> add( int index, E element )
{
stackTrace = new Throwable();
}
public E get( int index )
{
stackTrace = new Throwable();
if( index < 0 || index >= this.size() )
{
throw new IndexOutOfBoundsException();
}
return next.get( index );
}
public E set( int index, E element )
{
stackTrace = new Throwable();
return next.set( index, element );
}
public int indexOf( E element )
{
stackTrace = new Throwable();
return next.indexOf( element );
}
public Node<E> remove( int index )
{
stackTrace = new Throwable();
return next.remove( index );
}
}