java LinkedList Make all the necessary changes to the class LinkedList in order
ID: 3809569 • Letter: J
Question
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:
Explanation / Answer
Here is the modified code for Iterator with start and stop indices. Since the class file NoSuchElementException is not pasted here, I assume you already have it. A sample program showing the iterator in used is also given with output. Please do rate the answer if it helped. Thank you very much.
LinkedList.java
public class LinkedList<E> {
// Objects of the class Elem are used to store the elements of the
// list.
private static class Elem<T> {
private final T value;
private Elem<T> previous;
private Elem<T> next;
private Elem(T value, Elem<T> previous, Elem<T> 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<E> {
private Elem<E> current;
private int current_index;
private int end_index;
private LinkedListIterator() {
this(0,size-1);
}
private LinkedListIterator(int start,int stop)
{
current = head;
current_index=-1;
for (; current_index<start-1; current_index++) {
current = current.next;
}
end_index = stop;
}
public E next() {
if(current_index == end_index)
{
throw new NoSuchElementException();
}
current_index ++;
current = current.next ; // move the cursor forward
return current.value ;
}
public boolean hasNext() {
return current_index != end_index;
}
}
private final Elem<E> head;
private int size;
public LinkedList() {
head = new Elem<E>(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<E> 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<E> iterator(int stop) {
return iterator(0,stop);
}
/**
* 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<E> iterator(int start, int stop) {
if(stop <0 || stop >= size || start<0 || stop >=size || start>stop)
new IndexOutOfBoundsException("start = "+start+" stop="+stop);
return new LinkedListIterator(start, stop);
}
/** 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<E> before, E obj) {
Elem<E> after = before.next;
before.next = new Elem<E>(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<E> before;
before = head;
for (int i=0; i<pos; i++) {
before = before.next;
}
addAfter(before, obj);
}
// Helper method. Removes the specified node.
private void remove(Elem<E> current) {
Elem<E> 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<E> current;
current = head.next;
for (int i=0; i<pos; i++) {
current = current.next;
}
remove(current);
}
/** Returns the element found at the specied position.
*
* @param pos the specified position
* @return the element found at the specified position
* @throws IndexOutOfBoundsException if the specified position is out of range
*/
public E get(int pos) {
if (pos < 0 || pos >= size) {
throw new IndexOutOfBoundsException(Integer.toString(pos));
}
Elem<E> current;
current = head.next;
for (int i=0; i<pos; i++) {
current = current.next;
}
return current.value;
}
/** Returns a String representation of this list.
*
* @return a String representation of this list
*/
public String toString() {
StringBuffer str = new StringBuffer("{");
Elem<E> p = head.next;
while (p != head) {
str.append(p.value);
if (p.next != head) {
str.append(",");
}
p = p.next;
}
str.append("}");
return str.toString();
}
}
TestIterator.java
public class TestIterator {
public static void displayIterator(Iterator<String> iter){
;
while(iter.hasNext())
System.out.println(iter.next());
System.out.println("Finished iterating");
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
Iterator<String> iter=list.iterator();
System.out.println("Empty list default iteration");
displayIterator(iter);
list.addLast("hello");
list.addLast("everybody");
list.addLast("good");
list.addLast("morning");
iter=list.iterator();
System.out.println("list with 4 words default iteration");
displayIterator(iter);
iter=list.iterator(2, 3);
System.out.println("list with 4 words iterator(start=2, stop=3)");
displayIterator(iter);
}
}
output
Empty list default iteration
Finished iterating
list with 4 words default iteration
hello
everybody
good
morning
Finished iterating
list with 4 words iterator(start=2, stop=3)
good
morning
Finished iterating
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.