Hello, I am a beginner to java and needed some assistance...I am writing code to create my own singly linked list, and an iterator that iterates over the elements of the list...however I don't know how to use the iterator to display the elements of the list and I am not sure if my implementation is correct to begin with...everytime i call the iterator it keeps printing "Jack" so I don't know if one of my methods is wrong or my iterator is wrong...please help me out!! thank you so much!!

Heres the code for my singly linked list

import java.util.NoSuchElementException;

public class LinkedList<E>
{
	private static class Node<E>
	{
		public E data;
		public Node<E> next;
		
		public Node(E data, Node<E> next)
		{
			this.data = data;
			this.next = next;
		}
		
	}


	
	private Node<E> head;
	private Node<E> current;
	private Node<E> previous;
	private int count;

	public LinkedList()
	{
		head = null;
		count = 0;
	}


	
	public void addToFront(E e)
	{
		Node<E> newNode = new Node<E> (e, head);
		head = newNode;
		count++;
		
	}
	
	public void remove(E e)
	{
		current = head.next;
		previous = head;
		if (head.equals(e))
		{
			head = head.next;
			count--;
			return;
		}
		else if (!head.equals(e))
		{
			while (!(current.next == null))
			{
            if (current.equals(e))
            {
            previous.next = current.next;
                        count--;
                        return;
             }
                previous = previous.next;
                current = current.next;			 
             }
}

else 
{
        throw new NoSuchElementException();
}
		}
	    
	
	public int size()
	{
		return count;
		
	}
	
	public void clear()
	{
		head = null;
		count = 0;
	}

	public Iterator iterator()
	{
		return new Iterator();
	}
	
	public class Iterator
	{
		
        private Node<E> currentSpot;  
		
		public Iterator()
		{
			currentSpot = head;
		}
		
		public boolean hasNext()
		{
			if (currentSpot != null)
			{
				return true;
			}
			else 
				return false;
		}
		
		public E next() 
		{ 
			 if (currentSpot != null) 
			 	{
				 E data = currentSpot.data;
                 currentSpot = currentSpot.next;
                 return data;
			 	 }
         throw new NoSuchElementException(); 	
		}
	}
}

And here is the main im trying to test my code in

public class LinkedListTest
{

	
	public static void main(String[] args)
	{
		LinkedList<String> theList = new LinkedList<String>();
		theList.addToFront("Ted");
		theList.addToFront("Fred");
		theList.addToFront("Sid");
		theList.addToFront("Jack");
		System.out.println(theList.iterator().next());
		System.out.println(theList.iterator().next());
		
	}
}

Hi, your methods should look like this:

import java.util.Collection;
import java.util.Iterator;

public class SingleList<E> implements Collection<E> {


	@Override
	public boolean add(E e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean contains(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Iterator<E> iterator() {
		return new SingleListIterator<E>();
	}

	@Override
	public boolean remove(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object[] toArray() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T[] toArray(T[] a) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private class SingleListIterator<E> implements Iterator<E> {

		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public E next() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void remove() {
			// TODO Auto-generated method stub
			
		}
		
	}	
	
}

Your single list should mimic this:

Head -> Data -> Data -> ...

With data added to the end, no indexing, and nothing referencing previous. If you want to find a piece of data, you have to start at the head and go one-by-one until you find it.

Iterators sit between data pieces and start to the left of the head:

/I\ [HEAD] - [DATA1] - [DATA2] - [DATA3]

E next() will have the iterator jump over the head, and land between the head and data1, along the way it will read head, and return the value.

[HEAD] /I\ [DATA1] - [DATA2] - [DATA3]

boolean hasNext() will return true b/c there is data to the right of the iterator.
E remove() will remove the data to the left of the iterator. If this happens to be the head, the data to the right of the iterator becomes the head, if there is no data to the right, the head value becomes null.

[HEAD (DATA1)] /I\ [DATA2] - [DATA3]

Also, iterators should keep track of state. A remove() can only be called if next() has first been called, otherwise an IllegalStateException must be thrown. You should not be able to do: next() remove() remove(), this should thrown an exception.

On another note -- I remember this being a lab a some point in college, haha!

This article has been dead for over six months. Start a new discussion instead.