简体   繁体   中英

Creating an iterator for Linked List in java without using getEntry

Im trying to create an Iterator that works for an linkedList. I have already created the linked list which I think should work good but now I need help on creating the iterator. I'm trying to make it faster than one that would simply use getEntry() with a for loop, increasing by one each time, because using that method would mean I have to traverse the linked list for each element. I'm trying to solve it quicker than that but dont know where to get started. I know I need to create the next and hasnext methods but not sure how to. Also stuck on the constructor and instance methods.

Here's the code I have so far:

import java.util.NoSuchElementException;


public class SListIterator<T>
{
    private Node firstNode;
    private int numberOfEntries;

    public SListIterator()
    {
        firstNode = null;
        numberOfEntries = 0;
    }

    public void addToFirst(T aData)
    {
        firstNode = new Node(aData, firstNode);
        numberOfEntries++;
    }

    public T getEntry(int givenPosition)
    {
        T result = null;

        if((givenPosition >= 1) && (givenPosition <= numberOfEntries))
        {
            result = (getNodeAt(givenPosition)).data;
        }

        return result;
    }

    private Node getNodeAt(int givenPosition)
    {
        Node currentNode = firstNode;

        for(int counter = 1; counter < givenPosition; counter++)
        {
            currentNode = currentNode.next;
        }

        return currentNode;
    }

    public Iterator<T> getIterator()
    {
        // TO DO        
    }

    private class IteratorForSList implements Iterator<T>
    {
        // instance variable for IteratorForSList       


        private IteratorForSList()
        {
            // constructor
        }

        public boolean hasNext()
        {
            // need help
        }

        public T next()
        {
            // need help
        }

        public T remove()
        {
            throw new UnsupportedOperationException("remove() is not supported by this iterator");
        }
    }

    private class Node
    {
        private T data;
        private Node next;

        private Node(T aData, Node nextNode)
        {
            data = aData;
            next = nextNode;
        }
    }
}

Your iterator only needs to know the current node in your linked list:

public Iterator<T> getIterator() {
  return new IteratorForSList(firstNode);
}

private class IteratorForSList implements Iterator<T> {
  private Node currentNode;

  private IteratorForSList(Node list) {
    currentNode = list;
  }

  public boolean hasNext() {
    return currentNode != null;
  }

  public T next() {
    T result = currentNode.data;
    currentNode = currentNode.next;
    return result;
  }

  public void remove() {
    throw new UnsupportedOperationException(
        "remove() is not supported by this iterator");
  }
}

Why not used java.util.LinkedList? It has an iterator method that returns an iterator.

import java.util.Iterator;
import java.util.LinkedList;


public class SListIterator{
    public static void main(String[] args){
        LinkedList<Integer> list = new LinkedList<Integer>();
        list.add(0);
        list.add(1);
        list.add(2);
        list.add(3);

        Iterator<Integer> iter = list.iterator();
        while(iter.hasNext()){
            System.out.println(iter.next());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM