SlideShare a Scribd company logo
1 of 9
Download to read offline
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 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;
}
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";
}
@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]
* 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;
}
}
/**
* 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;
}
}

More Related Content

Similar to I keep getting NullPointerExcepetion, can someone help me with spinL.pdf

Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdffacevenky
 
NumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdfNumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdfanjanacottonmills
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfalmonardfans
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfflashfashioncasualwe
 
LinkedListOperations.java-class Node{    public int item;  .pdf
LinkedListOperations.java-class Node{    public int item;  .pdfLinkedListOperations.java-class Node{    public int item;  .pdf
LinkedListOperations.java-class Node{    public int item;  .pdfaquastore223
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
Link list part 2
Link list part 2Link list part 2
Link list part 2Anaya Zafar
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdfaravlitraders2012
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfclimatecontrolsv
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdfharihelectronicspune
 
Submit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfSubmit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfakaluza07
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfABHISHEKREADYMADESKO
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
 

Similar to I keep getting NullPointerExcepetion, can someone help me with spinL.pdf (20)

Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
 
NumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdfNumberList.java (implements the linked list)public class NumberLis.pdf
NumberList.java (implements the linked list)public class NumberLis.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
 
LinkedListOperations.java-class Node{    public int item;  .pdf
LinkedListOperations.java-class Node{    public int item;  .pdfLinkedListOperations.java-class Node{    public int item;  .pdf
LinkedListOperations.java-class Node{    public int item;  .pdf
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
 
Hw3
Hw3Hw3
Hw3
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
 
PathOfMostResistance
PathOfMostResistancePathOfMostResistance
PathOfMostResistance
 
Submit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfSubmit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdf
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 

More from arkmuzikllc

Here is selection filepublic class Selection { public static in.pdf
Here is selection filepublic class Selection {  public static in.pdfHere is selection filepublic class Selection {  public static in.pdf
Here is selection filepublic class Selection { public static in.pdfarkmuzikllc
 
Help me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdfHelp me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdfarkmuzikllc
 
Hello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdfHello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdfarkmuzikllc
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfarkmuzikllc
 
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdfHINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdfarkmuzikllc
 
Hi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdfHi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdfarkmuzikllc
 
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdfGive an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdfarkmuzikllc
 
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdfIN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdfarkmuzikllc
 
Goal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdfGoal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdfarkmuzikllc
 
import React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdfimport React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdfarkmuzikllc
 
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdfim solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdfarkmuzikllc
 
Im creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfIm creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfarkmuzikllc
 
if any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdfif any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdfarkmuzikllc
 
I need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdfI need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdfarkmuzikllc
 
I have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfI have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfarkmuzikllc
 
I am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdfI am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdfarkmuzikllc
 
How can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdfHow can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdfarkmuzikllc
 
Hey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdfHey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdfarkmuzikllc
 

More from arkmuzikllc (18)

Here is selection filepublic class Selection { public static in.pdf
Here is selection filepublic class Selection {  public static in.pdfHere is selection filepublic class Selection {  public static in.pdf
Here is selection filepublic class Selection { public static in.pdf
 
Help me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdfHelp me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdf
 
Hello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdfHello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdf
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
 
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdfHINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
 
Hi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdfHi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdf
 
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdfGive an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdf
 
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdfIN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdf
 
Goal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdfGoal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdf
 
import React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdfimport React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdf
 
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdfim solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
 
Im creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfIm creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdf
 
if any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdfif any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdf
 
I need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdfI need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdf
 
I have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfI have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdf
 
I am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdfI am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdf
 
How can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdfHow can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdf
 
Hey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdfHey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdf
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
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; } }