Java iterator program with starter code? package list; public class List<E> { pr
ID: 3813537 • Letter: J
Question
Java iterator program with starter code?
package list;
public class List<E>
{
private Node first;
private Node last;
private long length;
private class Node
{
public E data;
public Node next;
public Node prev;
}
/**
* A generic inner ListIterator class that implements the
* ListIteratorAPI. It is used as a cursor to traverse the list.
*/
private class ListIterator implements ListIteratorAPI<E>
{
private Node position; // the node on the left of the iterator
private Node previousPosition; // the previous iterator position
private boolean isAfterNext;
private boolean isAfterPrevious;
/**
* Constructs an iterator that points to the front
* of the linked list.
*/
public ListIterator()
{
position = null;
previousPosition = null;
isAfterNext = false;
isAfterPrevious = false;
}
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the right of
* its current position (moves the iterator, in the forward direction,
* past the node on the right), and returns the data value in the node
* that the iterator just passed over.
*/
public E next() throws ListException
{
if (!hasNext())
throw new ListException("Called at the end of the list");
previousPosition = position; // Remember for remove.
if (position == null)
position = first;
else
position = position.next;
isAfterNext = true;
return position.data;
}
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the
* left of its current position (moves the iterator, in the backward
* direction, past the node on the left), and returns the data value
* in the node that the iterator just passed over.
*/
public E previous() throws ListException
{
// Implement this method.
}
/**
* Returns true if there is a node on the right of the iterator
* position; otherwise, returns false.
*/
public boolean hasNext()
{
if (position == null)
return first != null;
else
return position.next != null;
}
/**
* Returns true if the iterator position is not null;
* otherwise, returns false.
*/
public boolean hasPrevious()
{
// Implement this method.
}
/**
* Inserts a new node on the left of the iterator and resets
* the iterator position to this inserted node.
*/
public void add(E data)
{
// Implement this method.
}
/**
* Removes the last node passed over by the iterator in a call to
* either next or previous.
* Throws an exception if remove is not called immediately after a
* call to either next or previous.
*/
public void remove() throws ListException
{
// Implement this method.
}
/**
* Replaces the data value in the node on the left of the iterator
* with the input data value.
*/
public void set(E data) throws ListException
{
if (position == null)
throw new ListException("set call on a null node");
position.data = data;
}
}
/**
* Constructs an empty linked list.
*/
public List()
{
first = null;
last = null;
length = 0;
}
/**
* Adds an element to the front of the linked list.
* @param data the data value to add
*/
public void addFirst(E data)
{
Node newNode = new Node();
newNode.data = data;
if(length == 0)
{
first = newNode;
last = newNode;
}
else
{
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
}
/**
* Adds an element to the end of the linked list.
* @param data the data value to add
*/
public void addLast(E data)
{
// Implement this method.
}
/**
* Removes the first element in the linked list.
* @return the removed element
*/
public E removeFirst() throws ListException
{
if(length == 0)
throw new ListException("Non-empty list expected");
E data = first.data;
if(length == 1)
{
first = null;
last = null;
}
else
{
first = first.next;
first.prev = null;
}
length--;
return data;
}
/**
* Removes the last element in the linked list.
* @return the removed element
*/
public E removeLast() throws ListException
{
// Implement this method.
}
/**
* Returns the first element in the linked list.
* @return the first element in the linked list
*/
public E getFirst() throws ListException
{
if (first == null)
throw new ListException("Non-empty list expected");
return first.data;
}
/**
* Returns an iterator for iterating through this list.
* @return an iterator for iterating through this list
*/
public ListIteratorAPI listIterator()
{
return new ListIterator();
}
/**
* The number of elements in this list.
* @return the length of this list.
*/
public long size()
{
return length;
}
/**
* @return a string representation of the elements of this list
* in the format [e0, e1, ..., en-2, en-1], where each ei is a data
* value in the list and e0 is the data value in the first node
* and en-1 is the data value in the last node. It returns [] if this
* stack is empty.
*/
public String toString()
{
// Implement this method.
}
}
package list;
public interface ListIteratorAPI<E>
{
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the right of
* its current position (moves the iterator, in the forward direction,
* past the node on the right), and returns the data value in the node
* that the iterator just passed over.
*/
E next() throws ListException;
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the
* left of its current position (moves the iterator, in the backward
* direction, past the node on the left), and returns the data value
* in the node that the iterator just passed over.
*/
E previous() throws ListException;
/**
* Returns true if the iterator position is not null;
* otherwise, returns false.
*/
boolean hasNext();
/**
* Returns true if there is a node on the left of the iterator
* position; otherwise, returns false.
*/
boolean hasPrevious();
/**
* Inserts a new node on the left of the iterator and resets
* the iterator position to this inserted node.
*/
void add(E data);
/**
* Removes the last node passed over by the iterator in a call to
* either next or previous.
* Throws an exception if remove is not called immediately after a
* call to either next or previous.
*/
void remove() throws ListException;
/**
* Replaces the data value in the node on the left of the iterator
* with the input data value.
*/
void set(E data) throws ListException;
}
package list;
import java.util.NoSuchElementException;
public class ListException extends NoSuchElementException
{
public ListException(String msg)
{
super(msg);
}
}
need this output
c 1351 03.05, Spring 2017, Lab 10: Project List Juliet added to list Bob added to list Tom added to list Mary added to list [Mary, Tom Juliet Bob] list Mary removed from list Bob removed from list list [Tom Juliet] Iterator is at beginning of list Diana added to list list [Diana. Tom, Juliet Iterator passed Tom Iterator passed Juliet Harry added to list list [Diana Tom, Juliet Harryl Iterator moved to beginning of list Iterator passed Diana Iterator passed Tom Nina added to list Romeo added to list list [Diana Tom, Nina. Romeo, Juliet, Harryl Iterator passed Juliet Juliet removed from list list [Diana. Tom Nina Romeo, Harry] Iterator passed Romeo Iterator passed Nina Iterator passed Tom Tom removed from list list [Diana. Nina. Romeo, Harryl Iterator moved to beginning of list Forward version of list: Diana Nina Romeo Harry Backward version of list: Harry Nina. Diana Iterator is at beginning of list Iterator passed Diana Sara added to list list [Diana. Sara, Nina, Romeo Harryl Try to remove Sara from list: list. ListException remove not called immediately after next or previousExplanation / Answer
public class List<E>
{
private Node first;
private Node last;
private long length;
private class Node
{
public E data;
public Node next;
public Node prev;
}
/**
* A generic inner ListIterator class that implements the
* ListIteratorAPI. It is used as a cursor to traverse the list.
*/
private class ListIterator implements ListIteratorAPI<E>
{
private Node position; // the node on the left of the iterator
private Node previousPosition; // the previous iterator position
private boolean isAfterNext;
private boolean isAfterPrevious;
/**
* Constructs an iterator that points to the front
* of the linked list.
*/
public ListIterator()
{
position = null;
previousPosition = null;
isAfterNext = false;
isAfterPrevious = false;
}
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the right of
* its current position (moves the iterator, in the forward direction,
* past the node on the right), and returns the data value in the node
* that the iterator just passed over.
*/
public E next() throws ListException
{
if (!hasNext())
throw new ListException("Called at the end of the list");
previousPosition = position; // Remember for remove.
if (position == null)
position = first;
else
position = position.next;
isAfterNext = true;
return position.data;
}
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the
* left of its current position (moves the iterator, in the backward
* direction, past the node on the left), and returns the data value
* in the node that the iterator just passed over.
*/
public E previous() throws ListException
{
// Implement this method.
}
/**
* Returns true if there is a node on the right of the iterator
* position; otherwise, returns false.
*/
public boolean hasNext()
{
if (position == null)
return first != null;
else
return position.next != null;
}
/**
* Returns true if the iterator position is not null;
* otherwise, returns false.
*/
public boolean hasPrevious()
{
// Implement this method.
}
/**
* Inserts a new node on the left of the iterator and resets
* the iterator position to this inserted node.
*/
public void add(E data)
{
// Implement this method.
}
/**
* Removes the last node passed over by the iterator in a call to
* either next or previous.
* Throws an exception if remove is not called immediately after a
* call to either next or previous.
*/
public void remove() throws ListException
{
// Implement this method.
}
/**
* Replaces the data value in the node on the left of the iterator
* with the input data value.
*/
public void set(E data) throws ListException
{
if (position == null)
throw new ListException("set call on a null node");
position.data = data;
}
}
/**
* Constructs an empty linked list.
*/
public List()
{
first = null;
last = null;
length = 0;
}
/**
* Adds an element to the front of the linked list.
* @param data the data value to add
*/
public void addFirst(E data)
{
Node newNode = new Node();
newNode.data = data;
if(length == 0)
{
first = newNode;
last = newNode;
}
else
{
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
}
/**
* Adds an element to the end of the linked list.
* @param data the data value to add
*/
public void addLast(E data)
{
// Implement this method.
}
/**
* Removes the first element in the linked list.
* @return the removed element
*/
public E removeFirst() throws ListException
{
if(length == 0)
throw new ListException("Non-empty list expected");
E data = first.data;
if(length == 1)
{
first = null;
last = null;
}
else
{
first = first.next;
first.prev = null;
}
length--;
return data;
}
/**
* Removes the last element in the linked list.
* @return the removed element
*/
public E removeLast() throws ListException
{
// Implement this method.
}
/**
* Returns the first element in the linked list.
* @return the first element in the linked list
*/
public E getFirst() throws ListException
{
if (first == null)
throw new ListException("Non-empty list expected");
return first.data;
}
/**
* Returns an iterator for iterating through this list.
* @return an iterator for iterating through this list
*/
public ListIteratorAPI listIterator()
{
return new ListIterator();
}
/**
* The number of elements in this list.
* @return the length of this list.
*/
public long size()
{
return length;
}
/**
* @return a string representation of the elements of this list
* in the format [e0, e1, ..., en-2, en-1], where each ei is a data
* value in the list and e0 is the data value in the first node
* and en-1 is the data value in the last node. It returns [] if this
* stack is empty.
*/
public String toString()
{
// Implement this method.
}
}
package list;
public interface ListIteratorAPI<E>
{
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the right of
* its current position (moves the iterator, in the forward direction,
* past the node on the right), and returns the data value in the node
* that the iterator just passed over.
*/
E next() throws ListException;
/**
* Resets the iterator's previousPosition to its current position,
* resets the iterator's current position to the node on the
* left of its current position (moves the iterator, in the backward
* direction, past the node on the left), and returns the data value
* in the node that the iterator just passed over.
*/
E previous() throws ListException;
/**
* Returns true if the iterator position is not null;
* otherwise, returns false.
*/
boolean hasNext();
/**
* Returns true if there is a node on the left of the iterator
* position; otherwise, returns false.
*/
boolean hasPrevious();
/**
* Inserts a new node on the left of the iterator and resets
* the iterator position to this inserted node.
*/
void add(E data);
/**
* Removes the last node passed over by the iterator in a call to
* either next or previous.
* Throws an exception if remove is not called immediately after a
* call to either next or previous.
*/
void remove() throws ListException;
/**
* Replaces the data value in the node on the left of the iterator
* with the input data value.
*/
void set(E data) throws ListException;
}
package list;
import java.util.NoSuchElementException;
public class ListException extends NoSuchElementException
{
public ListException(String msg)
{
super(msg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.