Question
import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedListWrapper { public static interface List extends Iterable { public int size(); public boolean isEmpty(); public boolean isMember(E e);
import java.util.Iterator; import java.util.NoSuchElementException;
public class LinkedListWrapper { public static interface List
public void add(E e); public void add(E e, int index); public E get(int index); public E remove(int index); public boolean remove(E e); public int removeAll(E e); public E replace(int index, E newElement); public void clear(); public Object[] toArray(); public void addBefore(E e, E f);
} public static class SinglyLinkedList
@SuppressWarnings("hiding") private class SinglyLinkedListIterator
@SuppressWarnings("unchecked") public SinglyLinkedListIterator() { this.nextNode = (Node
@Override public E next() { if (this.hasNext()) { E result = this.nextNode.getElement(); this.nextNode = this.nextNode.getNext(); return result; } else { throw new NoSuchElementException(); } } } private static class Node
}
private Node
@Override public boolean isEmpty() { return this.size() == 0; }
@Override public boolean isMember(E e) { return this.firstIndexOf(e) >= 0; }
@Override public int firstIndexOf(E e) { int i = 0; for (Node
@Override public void add(E e) { if (this.isEmpty()) { this.header.setNext(new Node
@Override public void add(E e, int index) { if ((index this.currentSize)) { throw new IndexOutOfBoundsException(); } if (index == this.currentSize) { this.add(e); } else { Node
@Override public E get(int position) { if ((position = this.currentSize) { throw new IndexOutOfBoundsException(); } Node
private Node
} @Override public E remove(int index) { if ((index = this.currentSize)){ throw new IndexOutOfBoundsException(); } else { Node
@Override public E replace(int position, E newElement) { if ((position = this.currentSize) { throw new IndexOutOfBoundsException(); } Node
@Override public void clear() { while(!this.isEmpty()) { this.remove(0); } }
@Override public Object[] toArray() { Object[] result = new Object[this.size()]; for (int i=0; i
@Override public Iterator
@Override public int lastIndexOf(E e) { int i = 0, result = -1; for (Node
@Override public boolean remove(E e) { int i = this.firstIndexOf(e); if (i
@Override public int removeAll(E e) { int count = 0; while (this.remove(e)) { count++; } return count; }
@Override public void addBefore(E e, E f) { for(int i=0;i } } } } }
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