Java 双链接列表上一个实例变量

Java 双链接列表上一个实例变量,java,linked-list,doubly-linked-list,Java,Linked List,Doubly Linked List,在我的java编程课上,我们学习了双链表,我得到了一个对象中3个节点指向另一个对象的要点。然而,在他给我们的实验室里,我完全不知道如何使链表中的节点指向上一个节点。我见过其他程序做同样的事情,但我不知道如何将其合并到我自己的代码中。我曾考虑过使用“previousNode对象”,但不确定这样做是否正确。所以我的问题是如何创建指向前一个节点的指针,以及指向新节点的前一个节点?注意:新节点将添加到列表的末尾 方法在链表中添加元素 class ElementList { Element

在我的java编程课上,我们学习了双链表,我得到了一个对象中3个节点指向另一个对象的要点。然而,在他给我们的实验室里,我完全不知道如何使链表中的节点指向上一个节点。我见过其他程序做同样的事情,但我不知道如何将其合并到我自己的代码中。我曾考虑过使用“previousNode对象”,但不确定这样做是否正确。所以我的问题是如何创建指向前一个节点的指针,以及指向新节点的前一个节点?注意:新节点将添加到列表的末尾

方法在链表中添加元素

class ElementList
{
 Element       firstNode;
 public ElementList()
{
  this.firstNode = null;
}

 public void addElement( String first, String last, long number )
{
  Element    previousNode, newNode, currentNode;

  newNode = new Element( first, last, number );

  if ( this.firstNode == null)     // Determine if there is a firstNode
  {
     this.firstNode = newNode;      // Store the first node
  }
  else
  {
     currentNode = this.firstNode;  // Assign temporary element to first
     while ( currentNode.nextElement != null )  // Check if this is last element or header
     {
        currentNode = currentNode.nextElement;  // Go to the next element
     }
     currentNode.nextElement = newNode;         // Point last element of list to newNode

  }
}
以下是完整源代码的链接:

以下是整个作业的链接:

首先,您可以通过在
ElementList
类中设置
lastNode
字段来避免while循环:

class ElementList {
    Element firstNode;
    Element lastNode;

    public ElementList() {
        this.firstNode = null;
        this.lastNode = null;
    }

    public void addElement(String first, String last, long number) {
        Element previousNode, newNode, currentNode;

        newNode = new Element(first, last, number);

        if (this.firstNode == null) // Determine if there is a firstNode
        {
            this.firstNode = newNode;      // Store the first node
            this.lastNode = newNode;       // ... and the last node
        } else {
            this.lastNode.nextElement = newNode;
            this.lastNode = newNode;
        }
    }
}
目前它仍然是一个单链接列表,但您可以在
元素
类中添加一个
previousElement
字段,并稍微更改
addElement
方法:

    public void addElement(String first, String last, long number) {
        Element previousNode, newNode, currentNode;

        newNode = new Element(first, last, number);

        if (this.firstNode == null) // Determine if there is a firstNode
        {
            this.firstNode = newNode;      // Store the first node
            this.lastNode = newNode;       // ... and the last node
        } else {
            this.lastNode.nextElement = newNode;
            newNode.previousElement = this.lastNode;
            this.lastNode = newNode;
        }
    }
现在,您可能需要编写一个删除节点的方法:

public void removeElement(Element node) {
    if (node.nextElement == null) {
        // node is the last node
        this.lastNode = node.previousElement;
        if (this.lastNode != null) {
            this.lastNode.nextElement = null;
        }
    } else {
        node.nextElement.previousElement = node.previousElement;
    }
    if (node.previousElement == null) {
        // node is the first node
        this.firstNode = node.nextElement;
        if (this.firstNode != null) {
            this.firstNode.previousElement = null;
        }
    } else {
        node.previousElement.nextElement = node.nextElement;
    }
}

非常感谢你!这确实帮了大忙!