Java 通用单链表的实现
新的网站。尝试实现一个通用的SingleyLinkedList时,fetch返回null,即使在delete方法中有节点,但返回false,而预期为true。此外,当我决定按相反顺序删除时,它的功能也很好。寻找一双新鲜的眼睛,看看我错过了什么。提前谢谢Java 通用单链表的实现,java,Java,新的网站。尝试实现一个通用的SingleyLinkedList时,fetch返回null,即使在delete方法中有节点,但返回false,而预期为true。此外,当我决定按相反顺序删除时,它的功能也很好。寻找一双新鲜的眼睛,看看我错过了什么。提前谢谢 public class SinglyLinkedList<T> { private Node<T> h; // list header public SinglyLinkedList() {
public class SinglyLinkedList<T> {
private Node<T> h; // list header
public SinglyLinkedList() {
h = new <T> Node(); // dummy node
h.l = null;
h.next = null;
}
public boolean insert(T newNode) {
Node n = new Node();
GenericNode node = (GenericNode) newNode;
if (node == null) // out of memory
{
return false;
} else {
n.next = h.next;
h.next = n;
n.l = (T) node.deepCopy();
return true;
}
}
public GenericNode fetch(Object targetKey) {
Node p = h.next;
GenericNode node = (GenericNode) p.l; // this is where am I think there is a problem. Is this right?
while (p != null && !(node.compareTo(targetKey) == 0)) {
p = p.next;
}
if (p != null) {
return node.deepCopy();
} else {
return null;
}
}
public boolean delete(Object targetKey) {
Node q = h;
Node p = h.next;
GenericNode node = (GenericNode)p.l;// I think is the problem
while (p != null && !(node.compareTo(targetKey) == 0)) {
q = p;
p = p.next;
}
if (p != null) {
q.next = p.next;
return true;
} else {
return false;
}
}
public boolean update(Object targetKey, T newNode) {
if (delete(targetKey) == false) {
return false;
} else if (insert(newNode) == false) {
return false;
}
return true;
}
public void showAll() {
Node p = h.next;
while (p != null) //continue to traverse the list
{
System.out.println(p.l.toString());
p = p.next;
}
}
/**
*
* @param <T>
*/
public class Node <T> {
private T l;
private Node <T> next;
public <T> Node() {
}
}// end of inner class Node
}
//end SinglyLinkedList outer class
公共类单链接列表{
私有节点h;//列表头
公共单链接列表(){
h=新节点();//虚拟节点
h、 l=零;
h、 next=null;
}
公共布尔插入(T newNode){
节点n=新节点();
GenericNode节点=(GenericNode)newNode;
if(node==null)//内存不足
{
返回false;
}否则{
n、 next=h.next;
h、 next=n;
n、 l=(T)node.deepCopy();
返回true;
}
}
公共GenericNode获取(对象targetKey){
节点p=h.next;
GenericNode=(GenericNode)p.l;//这就是我认为存在问题的地方。对吗?
while(p!=null&&!(node.compareTo(targetKey)==0)){
p=p.next;
}
如果(p!=null){
返回node.deepCopy();
}否则{
返回null;
}
}
公共布尔删除(对象targetKey){
节点q=h;
节点p=h.next;
GenericNode=(GenericNode)p.l;//我认为这就是问题所在
while(p!=null&&!(node.compareTo(targetKey)==0)){
q=p;
p=p.next;
}
如果(p!=null){
q、 next=p.next;
返回true;
}否则{
返回false;
}
}
公共布尔更新(对象targetKey,T newNode){
如果(删除(targetKey)=false){
返回false;
}else if(插入(newNode)==false){
返回false;
}
返回true;
}
公屋空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置{
节点p=h.next;
while(p!=null)//继续遍历列表
{
System.out.println(p.l.toString());
p=p.next;
}
}
/**
*
*@param
*/
公共类节点{
私人旅行社;
私有节点下一步;
公共节点(){
}
}//内部类节点的结束
}
//结束SingleLinkedList外部类
问题就在这里(在fetch
方法中):
您仅在进入循环之前分配节点
,并且仅在循环内更改p
值,因此节点
在整个循环中保持其初始值。你应使用:
GenericNode node = null; // define node before the loop in order to use it later
while (p != null) {
node = (GenericNode) p.l; // reset node value on each iteration
if (node.compareTo(targetKey) == 0) {
break;
}
p = p.next;
}
由于您在
delete
中有相同的代码,因此将应用相同的修复…这可能属于代码审阅页面:我们需要您的SingleLinkedList
的实现来告诉您失败的地方。我刚刚添加了它。我想我已经包含了它。若你们删除了最后一个元素,那个么即使删除了元素,你们也会得到false。这是故意的吗?正确地命名变量,编译器不关心您的节点
是否被称为h
或节点
,尽管人类很关心,因为更容易阅读和理解您的意图将此代码简化为NullPointerException。我开始想也许插入有问题?
GenericNode node = null; // define node before the loop in order to use it later
while (p != null) {
node = (GenericNode) p.l; // reset node value on each iteration
if (node.compareTo(targetKey) == 0) {
break;
}
p = p.next;
}