?
Solved

Where does the current variable come from in MyLinkedList?

Posted on 2011-10-26
6
Medium Priority
?
272 Views
Last Modified: 2012-05-12
In line 131, where does the current variable come from? I see it declared in line 116 as an instance of Node, but that declaration is in a different class and I'm wondering how my compiler recognizes current in line 131?

(see java code below)
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;
		public Node<AnyType>  next;
	}

	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 */

	private void addBefore( Node<AnyType> p, AnyType x ) {
		Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p ); 
		newNode.prev.next = newNode; 
		p.prev = newNode; 
		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;
	}


	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( ) 
        {
            if( modCount != expectedModCount )
                throw new java.util.ConcurrentModificationException( ); 
            if( !hasNext( ) )
                throw new java.util.NoSuchElementException( ); 
                   
            AnyType nextItem = current.data; //where did current come from?
            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 );
            okToRemove = false;  
            expectedModCount++;     
        }
    }
}

Open in new window

0
Comment
Question by:shampouya
6 Comments
 
LVL 6

Expert Comment

by:theKashyap
ID: 37032676
It should be in one of the #includes you have in this cpp file. You can also look at the pre-processed version (-E option on gcc if I recall correctly) of the cxx to find out.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 37032843
Your code example looks incomplete. See here for a working example.  http://pastebin.com/A8jQRxAN

@thekashyap
It is JAVA not CPP
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 37033081
Your curly braces are messed up. Look closely at lines 120 to 122
The curly brace on 122 is ending the one on 120 not the one on line 115.
So all the code from line 120 on is for the LinkedListIterator class.
The variable in question (current) was declared as a member of the class on line 116 so in all of the rest of the functions, that variable exists since it is part of the LinkedListIterator class.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:PCableGuy
ID: 37035925
I agree with TommySz, current is a private variable that is a member of the LinkedListIterator class.

The method public AnyType next( ) has access to current because it is also a member of the LinkedListIterator class.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 37037324
If you fix your curly braces, the whole thing makes a lot more sense.

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;
    public Node<AnyType>  next;
  }

  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 */

  private void addBefore( Node<AnyType> p, AnyType x ) 
  {
    Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p ); 
    newNode.prev.next = newNode; 
    p.prev = newNode; 
    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;
  }


  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( ) 
    {
        if( modCount != expectedModCount )
            throw new java.util.ConcurrentModificationException( ); 
        if( !hasNext( ) )
            throw new java.util.NoSuchElementException( ); 

        AnyType nextItem = current.data; //where did current come from?
        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 );
        okToRemove = false;  
        expectedModCount++;     
    }
  }
}

Open in new window

0
 

Author Closing Comment

by:shampouya
ID: 37042369
Thanks, I need to look at my braces more carefully!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Simple Linear Regression
Progress

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question