从双链表中删除节点?

时间:2021-01-21 07:18:04

Here's the method up to now

这是迄今为止的方法

public DNode removeDnode(DNode v, DNode e, DNode f)
{
    e = v.nextNode();
    f = v.prevNode();
    f.setNext() = e;
    e.setPrev() = f;
    v.setPrev(null);
    v.setNext(null);
    size = size - 1;
    return v;
}

Here's my node class

这是我的节点类

public class DNode
{

public String element;
public DNode next;
public DNode previous;

public DNode(String e, DNode n)
{
    element = e;
    next = n;
}    

public void setElement(String newElem) 
{ 
    element = newElem; 
}
public void setNext(DNode newNext) 
{ 
    next = newNext; 
}
public void setPrev(DNode newPrev)
{
    previous = newPrev;
}
public String getElement() 
{ 
    return element; 
}
public DNode nextNode() 
{
  return next;
}
public DNode prevNode() 
{
  return previous;
}

}

I'm getting the error 'method setNext in class DNode cannot be applied to given types but I don't know why. Any help?

我得到的错误'方法setNext在类DNode中不能应用于给定的类型,但我不知道为什么。有帮助吗?

(Don't read this this is to use characters so it will let me post)

(不要读这个是用字符所以它会让我发帖)

EDIT: new method below

编辑:下面的新方法

public DNode removeDnode(DNode current)
{
    DNode next = current.nextNode();
    DNode previous = current.prevNode();
    previous.setNext(next);
    next.setPrev(previous);
    current.setPrev(null);
    current.setNext(null);
    size = size - 1;
    return current;
}

but how do I deal with the end and beginning of the list

但是我该如何处理列表的结尾和开头

1 个解决方案

#1


2  

You need to do:

你需要这样做:

f.setNext(e);
e.setPrev(f);

#1


2  

You need to do:

你需要这样做:

f.setNext(e);
e.setPrev(f);