• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

What is the last remove method doing in LinkedList?

In line 146 of the Java code below, is that simply how you refer to a method in the outer class from within the inner class? In other words, line is 146 simply calling the remove method in line 79 using this format: OuterClass.this.method( argument)?
public class MyLinkedList<AnyType> implements Iterable<AnyType> {

	private int theSize;
	private int modCount = 0;
	private Node<AnyType> beginMarker;
	private Node<AnyType> endMarker;

	private static class Node<AnyType>
	{
		public Node( AnyType d, Node<AnyType> p, Node<AnyType> n ) {
			data = d; prev = p; next = n;}

		public AnyType data;
		public Node<AnyType>  prev; //instance of Node class inside the Node class
		public Node<AnyType>  next; //instance of Node class inside the Node class
	}

	public MyLinkedList( ) {
		clear( );}

	public void clear( ) // Change the size of this collection to zero.
	{    
		beginMarker = new Node<AnyType>( null, null, null );
		endMarker = new Node<AnyType>( null, beginMarker, null );
                beginMarker.next = endMarker;

		theSize = 0;
		modCount++;
	}

	public int size( ) { //Returns the number of items in this collection.
		return theSize;
	}

	public boolean isEmpty( ) {
		return size( ) == 0;} // test to see if this is true


	public boolean add( AnyType x ) { //Adds an item to this collection, at the end
		add( size( ), x );
		return true;
	}


	/* the add method adds an item to this collection, at specified position.
         Items at or after that 	position are slid one position higher */

	public void add( int idx, AnyType x ) {
		addBefore( getNode( idx), x );}


	public AnyType get( int idx ) { // Returns the item at position idx
		return getNode( idx ).data;}


	public AnyType set( int idx, AnyType newVal ) { //Changes the item at position idx
		Node<AnyType> p = getNode( idx ); //create node object
		AnyType oldVal = p.data; //create anytype object, define it as the node's old data
		p.data = newVal; //define node's data as the new value argument passed into method
		return oldVal; //return the old data, for whatever reason
	}

	public AnyType remove( int idx ) { //Removes an item from this collection
		return	remove(getNode( idx ));} //uses idx to call the remove method below


	/* this addBefore method adds an item to this collection, at specified position p.
	Items at or after that position are slid one position higher */

        //the correct p is sent courtesy of add and getNode
	private void addBefore( Node<AnyType> p, AnyType x ) {
		Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p ); //inserts new node btw
		newNode.prev.next = newNode; //moves back one node and redefines the next node
		p.prev = newNode; //redefines the node just prior to the right hand node
		theSize++;
		modCount++;
	}

	private AnyType remove( Node<AnyType> p ) {  // Removes the object contained in Node p.
		p.next.prev = p.prev;
		p.prev.next = p.next;
		theSize--;
		modCount++;
		return p.data;
	}


        //have to use loops in this method bc there are no arrays that allow jumping to an index
	private Node<AnyType> getNode( int idx)
	{
		Node<AnyType> p;

		if( idx < 0 || idx > size() )
            throw new IndexOutOfBoundsException();

        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 java.util.Iterator<AnyType> iterator( ) {
        return new LinkedListIterator( );
	}


	private class LinkedListIterator implements java.util.Iterator<AnyType>
        {
            private Node<AnyType> current = beginMarker.next;
            private int expectedModCount = modCount;
            private boolean okToRemove = false;

            public boolean hasNext( ) {
		return current != endMarker;
            }

            public AnyType next( ) //confusing
            {
                if( modCount != expectedModCount )
                    throw new java.util.ConcurrentModificationException( );
                if( !hasNext( ) )
                    throw new java.util.NoSuchElementException( );

                AnyType nextItem = current.data;
                current = current.next; //advances current node one spot forward
                okToRemove = true;
                return nextItem; //returns data of old node
            }

            public void remove( )
            {
                if( modCount != expectedModCount )
                    throw new java.util.ConcurrentModificationException( );
                if( !okToRemove )
                    throw new IllegalStateException( );

                MyLinkedList.this.remove( current.prev );
                okToRemove = false;
                expectedModCount++;
            }
        }
    }

Open in new window

0
shampouya
Asked:
shampouya
  • 3
  • 2
2 Solutions
 
farzanjCommented:
0
 
shampouyaAuthor Commented:
Separate question: In the program in the link that you just gave, is line 3 a way to treat doit() as the constructor for the TestIt class?
 
public class TestIt {
  public static void main(String a[]){
     new TestIt().doit();
     /*
      output :
      Hello world!
     */
  }
  public void doit() {
      new InnerClass().sayHello();
  }
  public void enclosingClassMethod(){
      System.out.println("Hello world!");
  }
 
 
 class InnerClass {
   public void sayHello() {
     TestIt.this.enclosingClassMethod();
   }
 }
}

Open in new window

0
 
TommySzalapskiCommented:
No. It's not a constructor. Saying new TestIt().doit(); is basically the same as
TestIt x = new TestIt();
x.doit();

The code is just shortcutting the process.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
shampouyaAuthor Commented:
This may be a stupid question, but in line 3 why don't they just write doit()?
0
 
TommySzalapskiCommented:
You'd have to ask someone from the Java zone that one. It does look like main is inside the class so there should be a TestIt lying around in main without needing to call one.

My guess is they did it that way to illustrate the normal way of doing it no matter if main is inside the class or not. They just put the main in the class to make the example shorter.
0
 
shampouyaAuthor Commented:
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now