Question
Modify the author's MyLinkedList class to add the following methods: 15 points each (a-e) a . swap receives two index positions as parameters, and swaps
Modify the author's "MyLinkedList" class to add the following methods: 15 points each (a-e) a. swap receives two index positions as parameters, and swaps the nodes at these positions by changing the links, provided both positions are within the current size
public class MyLinkedList
{
public MyLinkedList( )
{
doClear( );
}
private void clear( )
{
doClear( );
}
public void doClear( )
{
beginMarker = new Node<>( null, null, null );
endMarker = new Node<>( null, beginMarker, null );
beginMarker.next = endMarker;
theSize = 0;
modCount++;
}
public int size( )
{
return theSize;
}
public boolean isEmpty( )
{
return size( ) == 0;
}
public boolean add( AnyType x )
{
add( size( ), x );
return true;
}
public void add( int idx, AnyType x )
{
addBefore( getNode( idx, 0, size( ) ), x );
}
private void addBefore( Node
{
Node
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
}
public AnyType get( int idx )
{
return getNode( idx ).data;
}
public AnyType set( int idx, AnyType newVal )
{
Node
AnyType oldVal = p.data;
p.data = newVal;
return oldVal;
}
private Node
{
return getNode( idx, 0, size( ) - 1 );
}
private Node
{
Node
if( idx < lower || idx > upper )
throw new IndexOutOfBoundsException( "getNode index: " + idx + "; size: " + size( ) );
if( idx < size( ) / 2 )
{
p = beginMarker.next;
for( int i = 0; i < idx; i++ )
p = p.next;
}
else
{
p = endMarker;
for( int i = size( ); i > idx; i-- )
p = p.prev;
}
return p;
}
public AnyType remove( int idx )
{
return remove( getNode( idx ) );
}
private AnyType remove( Node
{
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
modCount++;
return p.data;
}
public String toString( )
{
StringBuilder sb = new StringBuilder( "[ " );
for( AnyType x : this )
sb.append( x + " " );
sb.append( "]" );
return new String( sb );
}
public java.util.Iterator
{
return new LinkedListIterator( );
}
private class LinkedListIterator implements java.util.Iterator
{
private Node
private int expectedModCount = modCount;
private boolean okToRemove = false;
public boolean hasNext( )
{
return current != endMarker;
}
public AnyType next( )
{
if( modCount != expectedModCount )
throw new java.util.ConcurrentModificationException( );
if( !hasNext( ) )
throw new java.util.NoSuchElementException( );
AnyType nextItem = current.data;
current = current.next;
okToRemove = true;
return nextItem;
}
public void remove( )
{
if( modCount != expectedModCount )
throw new java.util.ConcurrentModificationException( );
if( !okToRemove )
throw new IllegalStateException( );
MyLinkedList.this.remove( current.prev );
expectedModCount++;
okToRemove = false;
}
}
private static class Node
{
public Node( AnyType d, Node
{
data = d; prev = p; next = n;
}
public AnyType data;
public Node
public Node
}
private int theSize;
private int modCount = 0;
private Node
private Node
}
class TestLinkedList
{
public static void main( String [ ] args )
{
MyLinkedList
for( int i = 0; i < 10; i++ )
lst.add( i );
for( int i = 20; i < 30; i++ )
lst.add( 0, i );
lst.remove( 0 );
lst.remove( lst.size( ) - 1 );
System.out.println( lst );
java.util.Iterator
while( itr.hasNext( ) )
{
itr.next( );
itr.remove( );
System.out.println( lst );
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started