Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 implements Iterable

{

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 p, AnyType x )

{

Node newNode = new Node<>( x, p.prev, p );

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 p = getNode( idx );

AnyType oldVal = p.data;

p.data = newVal;

return oldVal;

}

private Node getNode( int idx )

{

return getNode( idx, 0, size( ) - 1 );

}

private Node getNode( int idx, int lower, int upper )

{

Node p;

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 )

{

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 iterator( )

{

return new LinkedListIterator( );

}

private class LinkedListIterator implements java.util.Iterator

{

private Node current = beginMarker.next;

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 p, Node n )

{

data = d; prev = p; next = n;

}

public AnyType data;

public Nodeprev;

public Nodenext;

}

private int theSize;

private int modCount = 0;

private Node beginMarker;

private Node endMarker;

}

class TestLinkedList

{

public static void main( String [ ] args )

{

MyLinkedList lst = new 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 itr = lst.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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions