Answered step by step
Verified Expert Solution
Question
1 Approved Answer
java LinkedList Make all the necessary changes to the class LinkedList in order to implement the following methods. Iterator iterator(stop). Returns an iterator for this
java LinkedList
Make all the necessary changes to the class LinkedList in order to implement the following methods.
Iterator iterator(stop). Returns an iterator for this list stopping at a specified position. When the element at the specified position has been returned, the method hasNext returns false, call to the method next will cause NoSuchElementException to be thrown.
Iterator iterator(start, stop). Returns an iterator for this list that starts at a specified position and stops at a specified position.
Files:
public interface Iterator{ /** * Returns true if the iteration has more elements. (In other * words, returns true if next would return an element rather than * throwing an exception.) * * @return true if the iterator has more elements. */ boolean hasNext(); /** * Returns the next element in the interation. * * @return the next element in the iteration. * @throws NoSuchElementException iteration has no more elements. */ E next(); }
public class LinkedList{ // Objects of the class Elem are used to store the elements of the // list. private static class Elem { private final T value; private Elem previous; private Elem next; private Elem(T value, Elem previous, Elem next) { this.value = value; this.previous = previous; this.next = next; } } // An inner (non-static) class is used to implement the interface // Iterator. private class LinkedListIterator implements Iterator { private Elem current; private LinkedListIterator() { current = head; } public E next() { if (current.next == head) { throw new NoSuchElementException(); } current = current.next ; // move the cursor forward return current.value ; } public boolean hasNext() { return current.next != head; } } private final Elem head; private int size; public LinkedList() { head = new Elem (null, null, null); head.next = head; head.previous = head; size = 0; } /** * Returns an iterator for this list. * * @return an iterator for this list */ public Iterator iterator() { return new LinkedListIterator(); } /** * Returns an iterator for this list stopping at a specified position. * * @param stop the index of the last element of the iteration * @return an iterator for this list */ public Iterator iterator(int stop) { throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); } /** * Returns an iterator for this list that starts at a specified * position and stops at a specified position. * * @param start the index of the first element of the iteration * @param stop the index of the last element of the iteration * @return an iterator for this list */ public Iterator iterator(int start, int stop) { throw new UnsupportedOperationException("IMPLEMENT THIS METHOD"); } /** Returns the size of the list. * * @return the size of the list */ public int size() { return size; } // Helper method. Adds an element to the list after the specified // node. private void addAfter(Elem before, E obj) { Elem after = before.next; before.next = new Elem (obj, before, after); after.previous = before.next; size++; } /** Inserts the specified element at the beginning of this list. * * @param obj the object to be added */ public void addFirst(E obj) { if (obj == null) { throw new NullPointerException(); } addAfter(head, obj); } /** Inserts the specified element at the end of this list. * * @param obj the object to be added */ public void addLast(E obj) { if (obj == null) { throw new NullPointerException(); } addAfter(head.previous, obj); } /** Inserts the specified element at a specified position of this list. * * @param pos the specified position * @param obj the object to be added * @throws IndexOutOfBoundsException if the specified position is out of range */ public void add(int pos, E obj) { if (obj == null) { throw new NullPointerException(); } if (pos < 0 || pos > size) { throw new IndexOutOfBoundsException(Integer.toString(pos)); } Elem before; before = head; for (int i=0; i current) { Elem before = current.previous, after = current.next; before.next = after; after.previous = before; size--; } /** Removes the first element from this list. */ public void removeFirst() { if (size == 0) { throw new NoSuchElementException(); } remove(head.next); } /** Removes the last element from this list. */ public void removeLast() { if (size == 0) { throw new NoSuchElementException(); } remove(head.previous); } /** Remove the element at the specified position. * * @param pos the specified position * @throws IndexOutOfBoundsException if the specified position is out of range */ public void remove(int pos) { if (pos < 0 || pos >= size) { throw new IndexOutOfBoundsException(Integer.toString(pos)); } Elem current; current = head.next; for (int i=0; i = size) { throw new IndexOutOfBoundsException(Integer.toString(pos)); } Elem current; current = head.next; for (int i=0; i p = head.next; while (p != head) { str.append(p.value); if (p.next != head) { str.append(","); } p = p.next; } str.append("}"); return str.toString(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started