[英]Removing a specific node from a single linked list
I posted a question yesterday about an issue I was having overriding the toString() for this program, but now I have a different problem. 昨天我发布了一个有关该程序重写toString()的问题,但是现在我遇到了另一个问题。 The removeItem() method is supposed to remove the node with the given data value (in this case a String name).
应该使用removeItem()方法删除具有给定数据值(在这种情况下为字符串名称)的节点。 I'm getting a NullPointerException on line 64 and I can't seem to figure it out for whatever reason.
我在第64行上收到NullPointerException,无论出于什么原因,我似乎都无法弄清楚。 My code is below, and thanks in advance for any help.
我的代码在下面,在此先感谢您的帮助。
public class StudentRegistration<E>
{
private static class Node<E>
{
/** The data value. */
private E data;
/** The link */
private Node<E> next = null;
/**
* Construct a node with the given data value and link
* @param data - The data value
* @param next - The link
*/
public Node(E data, Node<E> next)
{
this.data = data;
this.next = next;
}
/**
* Construct a node with the given data value
* @param data - The data value
*/
public Node(E data)
{
this(data, null);
}
public Node getNext()
{
return next;
}
public E getData()
{
return data;
}
public void setNext(Node append)
{
next = append;
}
}
/** A reference to the head of the list */
private Node<E> head = null;
/** The size of the list */
private int size = 0;
/** Helper methods */
/** Remove the first occurance of element item.
@param item the item to be removed
@return true if item is found and removed; otherwise, return false.
*/
public void removeItem(E item)
{
Node<E> position = head;
Node<E> nextPosition1,
nextPosition2;
while (position != null)
{
if(position.getNext().getData() == item) //NullPointerException
{
nextPosition1 = position.getNext();
nextPosition2 = nextPosition1.getNext();
position.setNext(nextPosition2);
}
else
{
position = position.getNext();
}
}
}
/** Insert an item as the first item of the list.
* @param item The item to be inserted
*/
public void addFirst(E item)
{
head = new Node<E>(item, head);
size++;
}
/**
* Remove the first node from the list
* @returns The removed node's data or null if the list is empty
*/
public E removeFirst()
{
Node<E> temp = head;
if (head != null)
{
head = head.next;
}
if (temp != null)
{
size--;
return temp.data;
} else
{
return null;
}
}
/** Add a node to the end of the list
*@param value The data for the new node
*/
public void addLast(E value)
{
// location for new value
Node<E> temp = new Node<E>(value,null);
if (head != null)
{
// pointer to possible tail
Node<E> finger = head;
while (finger.next != null)
{
finger = finger.next;
}
finger.setNext(temp);
} else head = temp;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("[");
Node<E> aux = this.head;
boolean isFirst = true;
while(aux != null)
{
if(!isFirst)
{
sb.append(", ");
}
isFirst = false;
sb.append(aux.data.toString());
aux=aux.next;
}
return sb.append("]").toString();
}
}
Until you are practised in "visualizing" data structures in your head, a good way to understand what is going on is to get a piece of paper and draw a "boxes and pointers" diagram representing the nodes in your data structure (and the relevant fields) ... the local variables in your diagram. 除非您练习过“可视化”头脑中的数据结构,否则要了解情况的好方法是拿一张纸,并画一个“框和指针”图来表示数据结构中的节点(以及相关的字段)...图表中的局部变量。 Then "hand execute" using a pencil and eraser 1 .
然后使用铅笔和橡皮1进行“手动执行”。
Don't worry. 不用担心 Linked list insertion and deletion is notoriously tricky for beginners.
众所周知,链表的插入和删除对于初学者来说是棘手的。 (That's why it is commonly set as a class exercise in introductory Java and algorithms classes.)
(这就是为什么在入门Java和算法类中通常将其设置为类练习的原因。)
1 - Note careful avoidance of International English faux-pas :-) 1-请注意避免使用国际英语faux-pas :-)
You have an exception when you reach the end and there is no next value. 到达终点时会有一个例外,没有下一个值。 You should be checking like this:
您应该像这样检查:
while (position.getNext() != null)
also use equals()
instead of ==
operatoor: 还可以使用
equals()
代替==
运算符:
if(position.getNext().getData().equals(item))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.