Код: Выделить всё
public void remove(E del) { //this deletes head node
if (size == 0) {
}
else {
Node temp = head; //start at head
head = head.next; //head points to head.next
temp = head.next; //move to the next node after head
temp.prev = null; //set prev equal to null
head.next = null; //setting head's next equal to null to make empty
}
}
public String toString() {
Node temp = head; //temp node starting at head to walk the list
if (temp == null) return ""; //if there is nothing to start at return nothing
String output = "[ ";
for(int i = 0; i < size; i++) { //cycle through while i < size
if(i == 0) { //if is index 0, then grab the data of head
output += temp.data;
}
else { //else if i is more than 0, then grab the data of head and display with output
output += ", \n" + temp.data;
}
temp = temp.next; // sets the temporary head equal to temp.next;
}
output += " ]";
return output;
}
Код: Выделить всё
remove()Код: Выделить всё
"NullPointerException: Cannot read field "data" because "temp" is null"Код: Выделить всё
toString()To remove the head node I figured that I would have to create a temp node that starts at the head node. After starting at the head node, I would use
Код: Выделить всё
head = head.nextКод: Выделить всё
head.next = nullИсточник: https://stackoverflow.com/questions/781 ... -head-node
Мобильная версия