[[[[Below is my answer.]]]] /** * Defines a doubly-linked list class * @author * @author */ import
Question:
[[[[Below is my answer.]]]]
/**
* 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 hello
*/
public LinkedList() {
first = null;
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 array form
*/
public LinkedList(T[] array) {
// Iterate over the elements in the array and add them to the LinkedList
for (T item : array) {
addLast(item);
}
}
/**
* 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
LinkedList
// Copy each element from the original list to the new list
Node current = original.first;
while (current != null) {
newList.addLast(current.data);
current = current.next;
}
// Update the fields of the current instance to match the new list
this.first = newList.first;
this.last = newList.last;
this.iterator = newList.iterator;
this.length = newList.length;
}
/**** ACCESSORS ****/
/**
* Returns the value stored in the first node
* @precondition get first data element
* @return the value stored at node first
* @throws NoSuchElementException no such element exception
*/
public T getFirst() throws NoSuchElementException {
if (first == null) throw new NoSuchElementException();
return first.data;
}
/**
* Returns the value stored in the last node
* @precondition get last data element
* @return the value stored in the node last
* @throws NoSuchElementException no such element exception
*/
public T getLast() throws NoSuchElementException {
if (last == null) throw new NoSuchElementException();
return last.data;
}
/**
* Returns the data stored in the iterator node
* @precondition get iterator
* @return the data stored in the iterator node
* @throw NullPointerException null pointer exception
*/
public T getIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
return iterator.data;
}
/**
* 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;
}
/**
* Returns whether the iterator is offEnd, i.e. null
* @return whether the iterator is null
*/
public boolean offEnd() {
return iterator == null;
}
/**** MUTATORS ****/
/**
* new first element
* @param data the data to insert at the front of the LinkedList
* @postcondition add first data element
*/
public void addFirst(T data) {
Node newNode = new Node(data);
if (length == 0) {
first = newNode;
last = newNode;
} else {
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
return;
}
/**
* Creates a new last element
* @param data the data to insert at the end of the LinkedList
* @postcondition add last data element
*/
public void addLast(T data) {
Node newNode = new Node(data);
if (length == 0) {
first = newNode;
last = newNode;
} else {
last.next = newNode;
newNode.prev = last;
last = newNode;
}
length++;
return;
}
public void addIterator(T data) throws NullPointerException{
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
Node newNode = new Node(data);
newNode.prev = iterator;
newNode.next = iterator.next;
if (iterator.next != null) {
iterator.next.prev = newNode;
}
iterator.next = newNode;
length++;
}
public void removeFirst() throws NoSuchElementException {
if (first == null) throw new NoSuchElementException();
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
first = first.next;
first.prev = first;
}
length--;
return;
}
public void removeLast() throws NoSuchElementException {
if (last == null) throw new NoSuchElementException();
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
if (iterator == last) {
iterator = null;
}
last = last.prev;
last.next = null;
}
length--;
return;
}
public void removeIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
if (iterator.prev != null) {
iterator.prev.next = iterator.next;
}
if (iterator.next != null) {
iterator.next.prev = iterator.prev;
}
if (iterator == first) {
first = iterator.next;
}
if (iterator == last) {
last = iterator.prev;
}
iterator = null;
length--;
}
public void positionIterator(){
iterator = first;
}
public void advanceIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
iterator = iterator.next;
}
public void reverseIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
iterator = iterator.prev;
}
/**** ADDITIONAL OPERATIONS ****/
/**
* Re-sets LinkedList to empty as if the
* default constructor had just been called
*/
public void clear() {
first = null;
last = null;
iterator = null;
length = 0;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
Node temp = first;
while (temp != null) {
result.append(temp.data).append(" ");
temp = temp.next;
}
return result.toString() + "\n";
}
@SuppressWarnings("unchecked") //good practice to remove warning here
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
LinkedList
if (this.length != otherList.length) return false;
Node thisCurrent = this.first;
Node otherCurrent = otherList.first;
while (thisCurrent != null) {
if (!thisCurrent.data.equals(otherCurrent.data)) {
return false;
}
thisCurrent = thisCurrent.next;
otherCurrent = otherCurrent.next;
}
return true;
}
/**CHALLENGE METHODS*/
public void spinList(int numMoves) throws IllegalArgumentException{
if (numMoves < 0) {
throw new IllegalArgumentException("numMoves cannot be negative");
}
if (length <= 1) {
// Nothing to spin if the list has 0 or 1 elements
return;
}
// Find the new last node after spinning
Node newLast = first;
for (int i = 0; i < length - 1; i++) {
newLast = newLast.next;
}
// Update the last node's next pointer to point to the first node
newLast.next = first;
// Update the first and last pointers
first = first.next;
last = newLast;
// Update the iterator position if it's not off the end
if (iterator != null) {
for (int i = 0; i < numMoves % length; i++) {
iterator = iterator.next;
}
}
}
public LinkedList
LinkedList
Node currentThis = this.first;
Node currentList = list.first;
while (currentThis != null || currentList != null) {
if (currentThis != null) {
result.addLast(currentThis.data);
currentThis = currentThis.next;
}
if (currentList != null) {
result.addLast(currentList.data);
currentList = currentList.next;
}
}
return result;
}
}
[[[[There is problem commnet. I am struggling right here. I don't know how to solve this problem.]]]]]]
Exception in thread "main" java.lang.NullPointerException at LinkedList.
How can I do for running without error?
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates