WallHero

TP04E04 SimpleLinkedListIterator

Oct 30th, 2020 (edited)
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Iterator;
  2.  
  3. public class SimpleLinkedListIterator<ELEMENT> implements Iterator<ELEMENT>
  4. {
  5.     Node<ELEMENT> current;
  6.    
  7.    
  8.     public SimpleLinkedListIterator(Node<ELEMENT> begin)
  9.     {
  10.         this.current = begin;
  11.     }
  12.    
  13.     @Override
  14.     public boolean hasNext() {
  15.         return this.current != null;
  16.     }
  17.  
  18.     @Override
  19.     public ELEMENT next() {
  20.         if(!this.hasNext()) return null;
  21.         ELEMENT item = this.current.item;
  22.         this.current = this.current.next;
  23.         return item;
  24.     }
  25.    
  26.     public Node<ELEMENT> nextNode()
  27.     {
  28.         if(!this.hasNext()) return null;
  29.         return this.current = this.current.next;
  30.     }
  31. }
  32.  
RAW Paste Data