The document provides instructions for implementing iterator methods in a LinkedList class. It describes updating the default constructor to initialize the iterator field to null, and modifying removeFirst() and removeLast() methods to handle cases where the iterator is at the first or last node. It then lists 8 steps to implement iterator methods like positionIterator(), offEnd(), getIterator(), advanceIterator(), reverseIterator(), addIterator(), and removeIterator(). The main() method creates a linked list and calls the iterator methods to test their functionality.
1. How do I fix it in LinkedList.java?
this is what i did
LabProgram.java
LinkedList.java:
/**
* Defines a doubly-linked list class
* @author
* @author
*/
import java.util.NoSuchElementException;
public class LinkedList {
private class Node {
private T data;
private Node next;
private Node prev;
public Node(T data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
private int length;
private Node first;
private Node last;
private Node iterator;
/**** CONSTRUCTORS ****/
/**
* Instantiates a new LinkedList with default values
* @postcondition
*/
public LinkedList() {
first = null;
2. last = null;
iterator = null;
length = 0;
}
/**
* Converts the given array into a LinkedList
* @param array the array of values to insert into this LinkedList
* @postcondition
*/
public LinkedList(T[] array) {
}
/**
* Instantiates a new LinkedList by copying another List
* @param original the LinkedList to copy
* @postcondition a new List object, which is an identical,
* but separate, copy of the LinkedList original
*/
public LinkedList(LinkedList original) {
}
/**** ACCESSORS ****/
public T getFirst() throws NoSuchElementException {
if (isEmpty()){
throw new NoSuchElementException("The list is empty");
}
return first.data;
}
public T getLast() throws NoSuchElementException {
if (isEmpty()){
throw new NoSuchElementException("The list is empty");
3. }
return last.data;
}
/**
* Returns the data stored in the iterator node
* @precondition
* @return the data stored in the iterator node
* @throw NullPointerException
*/
public T getIterator() throws NullPointerException {
if (iterator != null){
return iterator.data;
}else{
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
/**
* Returns the current length of the LinkedList
* @return the length of the LinkedList from 0 to n
*/
public int getLength() {
return length;
}
/**
* Returns whether the LinkedList is currently empty
* @return whether the LinkedList is empty
*/
public boolean isEmpty() {
return length == 0;
}
/**
4. * Returns whether the iterator is offEnd, i.e. null
* @return whether the iterator is null
*/
public boolean offEnd() {
return iterator == null;
}
/**** MUTATORS ****/
public void addFirst(T data) {
Node newNode = new Node(data);
if(isEmpty()){
first = newNode;
last = newNode;
}
else{
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
}
public void addLast(T data) {
Node newNode = new Node(data);
if(isEmpty()){
first = newNode;
last = newNode;
}
5. else{
last.next = newNode;
newNode.prev = last;
last = newNode;
}
length++;
}
/**
* Inserts a new element after the iterator
* @param data the data to insert
* @precondition
* @throws NullPointerException
*/
public void addIterator(T data) throws NullPointerException{
if(iterator != null){
Node newNode = new Node(data);
newNode.next = iterator.next;
iterator.next = newNode;
if (iterator == first){
first = newNode;
}
}else{
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
/
public void removeFirst() throws NoSuchElementException {
if(isEmpty()){
throw new NoSuchElementException("The list is empty");
6. }
if(length == 1){
first = null;
last = null;
iterator = null;
}
else{
if(iterator == first){
iterator = null;
}
first = first.next;
first.prev = null;
}
length--;
}
public void removeLast() throws NoSuchElementException {
if(isEmpty()){
throw new NoSuchElementException("The list is empty");
}
if(length == 1){
first = null;
last = null;
iterator = null;
}
else{
if(iterator == last){
iterator = null;
}
last = last.prev;
last.next = null;
7. }
length--;
}
/**
* removes the element referenced by the iterator
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void removeIterator() throws NullPointerException {
if(iterator != null){
if(iterator == first){
first = first.next;
}else{
Node prev = first;
while(prev.next != iterator){
prev = prev.next;
}
prev.next = iterator.next;
if (iterator == last){
last = prev;
}
}
iterator = null;
}else {
throw new NullPointerException("Iterator is off the end opf the list.");
8. }
}
/**
* places the iterator at the first node
* @postcondition
*/
public void positionIterator(){
iterator = first;
}
/**
* Moves the iterator one node towards the last
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void advanceIterator() throws NullPointerException {
if (!offEnd()){
iterator = iterator.next;
}else {
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
/**
* Moves the iterator one node towards the first
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void reverseIterator() throws NullPointerException {
if(iterator != first && iterator != null){
9. Node prev = first;
while (prev.next != iterator){
prev = prev.next;
}
iterator = prev;
}
}
public void clear() {
first = null;
last = null;
iterator = null;
length = 0;
}
public String toString() {
StringBuilder result = new StringBuilder();
Node temp = first;
while (temp != null){
result.append(temp.data + " ");
temp = temp.next;
}
10. return result.toString() + "n";
}
@SuppressWarnings("unchecked") //good practice to remove warning here
@Override
public boolean equals(Object obj) {
return false;
}
/**CHALLENGE METHODS*/
public void spinList(int numMoves) throws IllegalArgumentException{
}
public LinkedList altLists(LinkedList list) {
return null;
}
}
java.lang.NullPointerException
java.lang.NullPointerException
Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins
at null, as defined by the LinkedList constructors. Take a moment now to update your default
constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider
now what will happen if the iterator is at the first or last Node when these methods are called.
Update the methods to handle these edge cases now. We will develop additional methods during
this lab to work with the iterator. Remember to fill in the pre-and postconditions for each
method, as needed. Also inspect the LabProgram. java file and notice that the main () method of
the LabProgram creates a list of numbers and inserts each into a list. The main() method then
calls various iterator methods, displaying results of the method operation. Use Develop mode to
11. test your Linked List iterator code as you develop it. In Submit mode you will need to complete
all lab steps to pass all automatic tests. Step 2: Implement positionIterator() Method
positionIterator() moves the iterator to the beginning of the list. public void positionIterator() //
fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method
offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement
getIterator() Method getIterator() returns the element in the Node where the iterator is currently
located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator
forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method
reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement
additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement
removeIterator() Method removeIterator () removes the Node currently referenced by the iterator
and sets the iterator to null.
Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins
at null, as defined by the LinkedList constructors. Take a moment now to update your default
constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider
now what will happen if the iterator is at the first or last Node when these methods are called.
Update the methods to handle these edge cases now. We will develop additional methods during
this lab to work with the iterator. Remember to fill in the pre-and postconditions for each
method, as needed. Also inspect the LabProgram. java file and notice that the main () method of
the LabProgram creates a list of numbers and inserts each into a list. The main() method then
calls various iterator methods, displaying results of the method operation. Use Develop mode to
test your Linked List iterator code as you develop it. In Submit mode you will need to complete
all lab steps to pass all automatic tests. Step 2: Implement positionIterator() Method
positionIterator() moves the iterator to the beginning of the list. public void positionIterator() //
fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method
offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement
getIterator() Method getIterator() returns the element in the Node where the iterator is currently
located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator
forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method
reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement
additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement
removeIterator() Method removeIterator () removes the Node currently referenced by the iterator
and sets the iterator to null.
t java.util.scanner; c class LabProgram { ublic static void main(string[] args) { LabProgram lab