I keep getting NullPointerExcepetion, can someone help me with spinList(int numMoves) and
LinkedList altLists(LinkedList list)?
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;
public LinkedList() {
first = null;
last = null;
length = 0;
iterator = null;
}
public LinkedList(T[] array) {
if (array == null) {
return;
}
if (array.length == 0) {
length = 0;
first = null;
last = null;
iterator = null;
} else {
for (T item : array) {
addLast(item);
}
iterator = null;
}
}
public LinkedList(LinkedList original) {
if (original == null) {
return;
}
if (original.length == 0) {
length = 0;
first = null;
last = null;
iterator = null;
} else {
Node temp = original.first;
while (temp != null) {
addLast(temp.data);
temp = temp.next;
}
iterator = null;
}
}
public T getFirst() throws NoSuchElementException {
if(first == null){
throw new NoSuchElementException("getFirst(): List has no element to access");
}
return first.data;
}
public T getLast() throws NoSuchElementException {
if (last == null){
throw new NoSuchElementException("getLast(): List has no element to access");
}
return last.data;
}
public T getIterator() throws NullPointerException {
if (iterator == null){
throw new NullPointerException("getIterator(): There is no iterator to access");
}
return iterator.data;
}
public int getLength() {
return length;
}
public boolean isEmpty() {
return length == 0;
}
public boolean offEnd() {
return iterator == null;
}
public void addFirst(T data) {
if (length == 0){
first = last = new Node(data);
} else {
Node fill = new Node(data);
fill.next = first;
first.prev = fill;
first = fill;
}
length++;
}
public void addLast(T data) {
if (length == 0) {
first = last = new Node(data);
} else {
Node fill = new Node (data);
fill.prev = last;
last.next = fill;
last = fill;
}
length++;
}
public void addIterator(T data) throws NullPointerException{
if(iterator == null) {
throw new NullPointerException("addIterator(): Can't add the node because iterator points
to null");
} else if(iterator == last){
addLast(data);
} else{
Node fill = new Node(data);
iterator.next.prev = fill;
fill.prev = iterator;
fill.next = iterator.next;
iterator.next = fill;
length++;
}
}
public void removeFirst() throws NoSuchElementException {
if (length == 0) {
throw new NoSuchElementException("removeFirst(): Cannot remove from an empty
List");
} else if(length == 1) {
first = last = null;
} else {
first = first.next;
first.prev = null;
}
length--;
}
public void removeLast() throws NoSuchElementException {
if (length == 0){
throw new NoSuchElementException("removeLast(): Cannot remove from an empty
List");
} else if (length == 1){
first = last = null;
}else{
last = last.prev;
last.next = null;
}
length--;
}
public void removeIterator() throws NullPointerE.
The byproduct of sericulture in different industries.pptx
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
1. I keep getting NullPointerExcepetion, can someone help me with spinList(int numMoves) and
LinkedList altLists(LinkedList list)?
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;
public LinkedList() {
first = null;
last = null;
length = 0;
iterator = null;
}
public LinkedList(T[] array) {
if (array == null) {
return;
}
if (array.length == 0) {
length = 0;
first = null;
last = null;
iterator = null;
2. } else {
for (T item : array) {
addLast(item);
}
iterator = null;
}
}
public LinkedList(LinkedList original) {
if (original == null) {
return;
}
if (original.length == 0) {
length = 0;
first = null;
last = null;
iterator = null;
} else {
Node temp = original.first;
while (temp != null) {
addLast(temp.data);
temp = temp.next;
}
iterator = null;
}
}
public T getFirst() throws NoSuchElementException {
if(first == null){
throw new NoSuchElementException("getFirst(): List has no element to access");
}
return first.data;
}
public T getLast() throws NoSuchElementException {
if (last == null){
3. throw new NoSuchElementException("getLast(): List has no element to access");
}
return last.data;
}
public T getIterator() throws NullPointerException {
if (iterator == null){
throw new NullPointerException("getIterator(): There is no iterator to access");
}
return iterator.data;
}
public int getLength() {
return length;
}
public boolean isEmpty() {
return length == 0;
}
public boolean offEnd() {
return iterator == null;
}
public void addFirst(T data) {
if (length == 0){
first = last = new Node(data);
} else {
Node fill = new Node(data);
fill.next = first;
first.prev = fill;
first = fill;
}
length++;
}
4. public void addLast(T data) {
if (length == 0) {
first = last = new Node(data);
} else {
Node fill = new Node (data);
fill.prev = last;
last.next = fill;
last = fill;
}
length++;
}
public void addIterator(T data) throws NullPointerException{
if(iterator == null) {
throw new NullPointerException("addIterator(): Can't add the node because iterator points
to null");
} else if(iterator == last){
addLast(data);
} else{
Node fill = new Node(data);
iterator.next.prev = fill;
fill.prev = iterator;
fill.next = iterator.next;
iterator.next = fill;
length++;
}
}
public void removeFirst() throws NoSuchElementException {
if (length == 0) {
throw new NoSuchElementException("removeFirst(): Cannot remove from an empty
List");
} else if(length == 1) {
first = last = null;
} else {
first = first.next;
5. first.prev = null;
}
length--;
}
public void removeLast() throws NoSuchElementException {
if (length == 0){
throw new NoSuchElementException("removeLast(): Cannot remove from an empty
List");
} else if (length == 1){
first = last = null;
}else{
last = last.prev;
last.next = null;
}
length--;
}
public void removeIterator() throws NullPointerException {
if(iterator == null){
throw new NullPointerException("removeIterator(): Cannot remove the node because
iterator points to null");
} else if (iterator == first){
removeFirst();
} else if (iterator == last){
removeLast();
} else {
iterator.prev.next = iterator.next;
iterator.next.prev = iterator.prev;
length--;
}
iterator = null;
}
public void positionIterator(){
iterator = first;
6. }
public void advanceIterator() throws NullPointerException {
if(iterator == null){
throw new NullPointerException("advanceIterator(): Cannot advance because iterator
points to null");
} else {
iterator = iterator.next;
}
}
public void reverseIterator() throws NullPointerException {
if (iterator == null){
throw new NullPointerException("reverseIterator(): Cannot advance because iterator points
to numm");
} else {
iterator = iterator.prev;
}
}
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 + " ");
temp = temp.next;
}
return result.toString() + "n";
7. }
@SuppressWarnings("unchecked") //good practice to remove warning here
@Override public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (!(obj instanceof LinkedList)){
return false;
} else {
LinkedList list = (LinkedList) obj;
if (length != list.length){
return false;
} else {
Node temp1 = this.first;
Node temp2 = list.first;
while (temp1 != null) {
if (temp1.data == null || temp2.data == null){
if (temp1.data != temp2.data) {
return false;
}
temp1 = temp1.next;
temp2 = temp2.next;
}
return true;
}
}
}
return true;
}
/**
* Moves all nodes in the list towards the end
* of the list the number of times specified
* Any node that falls off the end of the list as it
* moves forward will be placed the front of the list
* For example: [1, 2, 3, 4, 5], numMoves = 2 -> [4, 5, 1, 2 ,3]
* For example: [1, 2, 3, 4, 5], numMoves = 4 -> [2, 3, 4, 5, 1]
8. * For example: [1, 2, 3, 4, 5], numMoves = 7 -> [4, 5, 1, 2 ,3]
* @param numMoves the number of times to move each node.
* @precondition numMoves >= 0
* @postcondition iterator position unchanged (i.e. still referencing
* the same node in the list, regardless of new location of Node)
* @throws IllegalArgumentException when numMoves < 0
*/
public void spinList(int numMoves) throws IllegalArgumentException{
if (numMoves < 0) {
throw new IllegalArgumentException("numMoves must be a positive number");
}
if (length <= 1) {
return;
}
numMoves = numMoves % length;
if (numMoves == 0) {
return;
}
Node newFirst = last;
Node newLast = first;
for (int i = 0; i < numMoves; i++) {
newFirst = newFirst.prev;
newLast = newLast.prev;
}
last.next = first;
first.prev = last;
newFirst.next = null;
newLast.prev = null;
first = newFirst;
last = newLast;
if (iterator != null && (iterator.next == null || iterator.prev == null)) {
iterator = null;
}
}
9. /**
* Splices together two LinkedLists to create a third List
* which contains alternating values from this list
* and the given parameter
* For example: [1,2,3] and [4,5,6] -> [1,4,2,5,3,6]
* For example: [1, 2, 3, 4] and [5, 6] -> [1, 5, 2, 6, 3, 4]
* For example: [1, 2] and [3, 4, 5, 6] -> [1, 3, 2, 4, 5, 6]
* @param list the second LinkedList
* @return a new LinkedList, which is the result of
* interlocking this and list
* @postcondition this and list are unchanged
*/
public LinkedList altLists(LinkedList list) {
LinkedList result = new LinkedList<>();
Node currentThis = this.first;
Node currentList = list.first;
while (currentThis != null && currentList != null) {
result.addLast(currentThis.data);
result.addLast(currentList.data);
currentThis = currentThis.next;
currentList = currentList.next;
}
while (currentList != null) {
result.addLast(currentThis.data);
currentThis = currentThis.next;
}
while(currentList != null){
result.addLast(currentList.data);
currentList = currentList.next;
}
return result;
}
}