Menu

(Solved) : Consider Following Implementation Linkedlist Method Iterator Returns Listlterator Listlter Q27144841 . . .

Consider the following implementation of LinkedList. It has a method iterator() which returns a Listlterator. A Listlterator produces the elements of the list in order 6. Example List Integer> li new LinkedList<>(); = lterator<Integer» i = list.iterator() while(i.hasNext()) System.out.print(i.next ()+,); The above code prints 4,33,16,4,5 Fix the implementation of hasNext and next (they are buggy). import java.util.Iterator; import java.lang. Iterable interface List<T> extends Iterable<T> // add ele to the end of the list void add (T ele) // return the element at index i T get (int i); class LinkedList<T implements List<T private ListNode head; @Override public void add (T ele)implementation omitted @override public T get (int i) { /* implementation omitted */ } @Override public Iterator<T iterator return new ListIterator (head); private class ListNode T data; ListNode next; /*rest of ListNode omitted/ private class ListIterator implements Iterator<T private ListNode cur; public ListIterator (ListNode start) cur = start ; public boolean hasNext( 1 return cur. next null ; public T next) cur cur.next; return cur.data;

Consider the following implementation of LinkedList. It has a method iterator() which returns a Listlterator. A Listlterator produces the elements of the list in order 6. Example List Integer> li new LinkedList<>(); = lterator<Integer» i = list.iterator() while(i.hasNext()) System.out.print(i.next ()+”,”); The above code prints 4,33,16,4,5 Fix the implementation of hasNext and next (they are buggy). import java.util.Iterator; import java.lang. Iterable interface List<T> extends Iterable<T> // add ele to the end of the list void add (T ele) // return the element at index i T get (int i); class LinkedList<T implements List<T private ListNode head; @Override public void add (T ele)implementation omitted @override public T get (int i) { /* implementation omitted */ } @Override public Iterator<T iterator return new ListIterator (head); private class ListNode T data; ListNode next; /*rest of ListNode omitted/ private class ListIterator implements Iterator<T private ListNode cur; public ListIterator (ListNode start) cur = start ; public boolean hasNext( 1 return cur. next null ; public T next) cur cur.next; return cur.data; Show transcribed image text

Expert Answer


Answer to Consider Following Implementation Linkedlist Method Iterator Returns Listlterator Listlter Q27144841 . . .

OR