1public class BasicLinkedList<T> implements Iterable<T> {
2 public int size;
3
4 private class Node {
5 private T data;
6 private Node next;
7
8 private Node(T data) {
9 this.data = data;
10 next = null;
11 }
12 }
13
14 private Node head;
15 private Node tail;
16
17 public BasicLinkedList() {
18 head = tail = null;
19 }
20//Add, remove method
21
22public Iterator<T> iterator() {
23 return new Iterator<T>() {
24
25 Node current = head;
26
27 @Override
28 public boolean hasNext() {
29 return current != null;
30 }
31
32 @Override
33 public T next() {
34 if(hasNext()){
35 T data = current.data;
36 current = current.next;
37 return data;
38 }
39 return null;
40 }
41
42 @Override
43 public void remove(){
44 throw new UnsupportedOperationException("Remove not implemented.");
45 }
46
47 };
48
1// Java code to illustrate listIterator()
2import java.io.*;
3import java.util.LinkedList;
4import java.util.ListIterator;
5
6public class LinkedListDemo {
7 public static void main(String args[])
8 {
9 // Creating an empty LinkedList
10 LinkedList<String> list = new LinkedList<String>();
11
12 // Use add() method to add elements in the list
13 list.add("Geeks");
14 list.add("for");
15 list.add("Geeks");
16 list.add("10");
17 list.add("20");
18
19 // Displaying the linkedlist
20 System.out.println("LinkedList:" + list);
21
22 // Setting the ListIterator at a specified position
23 ListIterator list_Iter = list.listIterator(2);
24
25 // Iterating through the created list from the position
26 System.out.println("The list is as follows:");
27 while(list_Iter.hasNext()){
28 System.out.println(list_Iter.next());
29 }
30 }
31}