Java 从单个链接列表中删除特定节点
昨天我发布了一个问题,关于我为这个程序重写toString()时遇到的一个问题,但现在我遇到了另一个问题。removeItem()方法应该删除具有给定数据值(在本例中为字符串名称)的节点。我在第64行得到了一个NullPointerException,但无论出于什么原因,我似乎都无法找到它。下面是我的代码,提前感谢您的帮助Java 从单个链接列表中删除特定节点,java,singly-linked-list,Java,Singly Linked List,昨天我发布了一个问题,关于我为这个程序重写toString()时遇到的一个问题,但现在我遇到了另一个问题。removeItem()方法应该删除具有给定数据值(在本例中为字符串名称)的节点。我在第64行得到了一个NullPointerException,但无论出于什么原因,我似乎都无法找到它。下面是我的代码,提前感谢您的帮助 public class StudentRegistration<E> { private static class Node<E>
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();
}
}
公共班级学生注册
{
私有静态类节点
{
/**数据值*/
私人电子数据;
/**链接*/
私有节点next=null;
/**
*用给定的数据值和链接构造一个节点
*@param data-数据值
*@param next-链接
*/
公共节点(E数据,节点下一个)
{
这个数据=数据;
this.next=next;
}
/**
*用给定的数据值构造一个节点
*@param data-数据值
*/
公共节点(E数据)
{
这是(数据,空);
}
公共节点getNext()
{
下一步返回;
}
公共E getData()
{
返回数据;
}
公共void setNext(节点附加)
{
下一步=追加;
}
}
/**对名单头的提及*/
私有节点头=null;
/**列表的大小*/
私有整数大小=0;
/**辅助方法*/
/**删除元素项的第一次出现。
@param item要删除的项
@如果找到并删除了项,则返回true;否则,返回false。
*/
公共无效删除项(E项)
{
节点位置=头部;
节点nextPosition1,
下一个位置2;
while(位置!=null)
{
if(position.getNext().getData()==item)//NullPointerException
{
nextPosition1=position.getNext();
nextPosition2=nextPosition1.getNext();
位置。设置下一个(下一个位置2);
}
其他的
{
position=position.getNext();
}
}
}
/**插入一项作为列表的第一项。
*@param item要插入的项
*/
公共无效添加优先(E项)
{
head=新节点(项目,head);
大小++;
}
/**
*从列表中删除第一个节点
*@返回已删除节点的数据,如果列表为空,则返回null
*/
公共E removeFirst()
{
节点温度=头;
if(head!=null)
{
head=head.next;
}
如果(温度!=null)
{
大小--;
返回温度数据;
}否则
{
返回null;
}
}
/**将节点添加到列表的末尾
*@param值新节点的数据
*/
公共void addLast(E值)
{
//新值的位置
节点温度=新节点(值,null);
if(head!=null)
{
//指向可能尾部的指针
节指=头;
while(finger.next!=null)
{
finger=finger.next;
}
finger.setNext(温度);
}否则水头=温度;
}
@凌驾
公共字符串toString()
{
StringBuilder sb=新的StringBuilder();
某人加上(“[”);
Node aux=this.head;
布尔值isFirst=true;
while(aux!=null)
{
如果(!isFirst)
{
某人加上(“,”);
}
isFirst=false;
sb.append(aux.data.toString());
aux=aux.next;
}
返回sb.append(“]”).toString();
}
}
当到达终点且没有下一个值时,会出现异常。
您应该这样检查:
while (position.getNext() != null)
也可使用equals()
代替=
运算符或:
if(position.getNext().getData().equals(item))
在你练习在头脑中“可视化”数据结构之前,了解发生了什么的一个好方法是拿一张纸,画一个“框和指针”图,表示数据结构中的节点(以及相关字段)。。。图中的局部变量。然后用铅笔和橡皮擦“手动执行” 别担心。链表的插入和删除对于初学者来说是出了名的棘手。(这就是为什么在介绍性Java和算法类中通常将其设置为类练习。)
1-注意小心避免国际英语失礼:-)换句话说:告诉我们“第64行”是哪一行。在那一行写上评论,或者给我们一些其他的提示。是的,我们可以将其复制到编辑器中并进行猜测,但我们不必这样做来获取此类基本信息。我的错误是,我添加了一条注释,您应该检查
位置。接下来的是空值而不是位置。啊,就是这样!谢谢大家我想我下次可能得试试这个。我最难想象这件事了。真的!!!哈哈,我目前正在波士顿大学攻读数据结构和算法的硕士学位,而拥有物理学学士学位并没有让我做好准备。我被难住了两天。。。