fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. MyLinkedList<Integer> l = new MyLinkedList<>();
  13. for (Integer v : l) {
  14. System.out.println(v);
  15. }
  16. }
  17. }
  18.  
  19. class MyLinkedList<T> implements Iterable<T> {
  20.  
  21. MyListNode head;
  22.  
  23. class MyListNode {
  24. public T value;
  25. public MyListNode next;
  26. }
  27.  
  28. public Iterator<T> iterator() {
  29. return new MyLinkedListIterator();
  30. }
  31.  
  32. private class MyLinkedListIterator implements Iterator<T> {
  33.  
  34. private MyListNode curr;
  35.  
  36. public MyLinkedListIterator() {
  37. this.curr = MyLinkedList.this.head;
  38. }
  39.  
  40. public boolean hasNext() {
  41. return this.curr != null;
  42. }
  43.  
  44. public T next() {
  45. if (this.hasNext()) {
  46. T value = curr.value;
  47. curr = curr.next;
  48. return value;
  49. }
  50. }
  51.  
  52. public void remove() {
  53. }
  54. }
  55. }
  56.  
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Standard output is empty