Results 1 to 3 of 3
  1. #1
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default need help creating my own iterator interface

    it's an expansion I have to make on a previous excercise...

    i know that code is working just fine... i just don't have a clue how to make an iterator...

    the task is to run over the list using an iterator

    i have made my interface( with the method hasNext() wich returns a boolean) but i just don't get wich class should implement the interface...
    MyList or Node

    i've added my design so you get an idea how my classes look like...

    any help is appreciated...

    Attached Thumbnails Attached Thumbnails need help creating my own iterator interface-design.jpg  
    Last edited by liluma; 10-09-2011 at 06:19 PM.

  2. #2
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default Re: need help creating my own iterator interface

    Can you post your code in the previous exercise?

    Regards,
    Jing-yi

  3. #3
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: need help creating my own iterator interface

    reply oke. :p

    this is the code for the class MyList
    Java Code:
    package domein;
    
    public class MyList <E>
    {
    	private Node<E> anker;
    	private Node<E> end;
    	private String name;	
    	
    	public MyList(String name)
    	{
    		this.name = name;
    	}
    	
    	public boolean isEmpty() 
    	{
    		return anker == null;
    	}
    
    	public String toString() 
    	{
    		if(isEmpty())
    			return name + "is empty\n";
    		
    		StringBuilder buffer = new StringBuilder();
    		buffer.append("the ").append(name).append(" is:\n");
    		Node<E> current = anker;
    		while(current != null)
    		{
    			buffer.append(current.getData()).append("\n");
    			current = current.getNextNode();
    		}
    		
    		//buffer.append("\n");
    		return buffer.toString();
    
                    //this should be wrewriting using the interface MyIterator
    	}
    
    	public void insertAtFront(E data) 
    	{
    		if(isEmpty())
    			anker = end = new Node<E>(data);
    		else
    		{
    			Node<E> aNode = new Node<E>(data);
    			aNode.setNextNode(anker);
    			anker = aNode;
    		}
    	}
    
    	public void insertAtBack(E data) 
    	{
    		if(isEmpty())
    			anker = end = new Node<E>(data);
    		else
    		{
    			end.setNextNode(new Node<E>(data));
    			end = end.getNextNode();
    		}		
    	}
    
    	public E removeFromFront() throws EmptyListException 
    	{
    		if(isEmpty())
    			throw new EmptyListException(name);
    		
    		E removedItem = anker.getData();
    		if(anker == end)
    			anker = end = null;
    		else
    			anker = anker.getNextNode();
    		
    		return removedItem;
    	}
    	
    	public E removeFromBack() throws EmptyListException
    	{
    		if(isEmpty())
    			throw new EmptyListException(name);
    		
    		E removedItem = end.getData();
    		Node<E> currentNode = anker;
    		do
    		{
    			currentNode = currentNode.getNextNode();
    		}while(currentNode.getNextNode() != end);
    		
    		end = currentNode;
    		
    		return removedItem;
    	}
    }

    and this is the code for the class Node
    Java Code:
    package domein;
    
    public class Node<E> //implements Comparable<E>
    {
    	private E data;
    	private Node<E> next;
    	private Node<E> leftNode;
    	private Node<E> rightNode;
    	
    	public Node(E data) 
    	{
    		this.data = data;
    	}
    	
    	public void setNextNode(Node<E> next)
    	{
    		this.next = next;
    	}
    	
    	public Node<E> getNextNode()
    	{
    		return next;
    	}
    	
    	public E getData()
    	{
    		return data;
    	}
    
    	public Node<E> getLeftNode() 
    	{
    		return leftNode;
    	}
    
    	public void setLeftNode(Node<E> leftNode) 
    	{
    		this.leftNode = leftNode;
    	}
    
    	public Node<E> getRightNode() 
    	{
    		return rightNode;
    	}
    
    	public void setRightNode(Node<E> rightNode) 
    	{
    		this.rightNode = rightNode;
    	}
    
    }
    think it's only this two classes that are relevant for my question...

Similar Threads

  1. creating an interface
    By Billaguana in forum New To Java
    Replies: 3
    Last Post: 01-16-2011, 11:05 PM
  2. Replies: 0
    Last Post: 05-18-2010, 01:53 AM
  3. Creating an Iterator for Linked List
    By spanky in forum New To Java
    Replies: 3
    Last Post: 10-05-2008, 08:16 PM
  4. Creating API Interface
    By ashanti in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:08 AM
  5. Creating a Thread using the Runnable interface
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •