更改双向链表中的通用类型节点的值

时间:2019-03-14 22:25:37

标签: java generics doubly-linked-list

我正在做一个练习,在检查节点的值是否在元素列表中之后,我想制作一种方法来更改节点的值。我尝试通过创建一个新对象newNode并使用node类中的setter方法更改值来做到这一点,但是我什么都没得到。我应该如何处理此问题以便更好地理解它。谢谢。

链接列表类别:

public class DLList<E> implements DLListADT<E> {

private DLNode<E> front; //. This is a reference to the first node of the doubly linked list.
private DLNode<E> rear; //. This is a reference to the last node of the doubly linked list.
private int count; //. The value of this variable is the number of data items in the linked list

public DLList() { // Creates an empty list.
    front = null;
    rear = null;
    count = 0;


    /** Changes the value of dataItem to newValue. An InvalidDataItemException is thrown if the given dataItem is not in the list. */

public void changeValue (E dataItem, int newValue) throws InvalidDataItemException {

        if (front == null) {
            throw new InvalidDataItemException("The specified element is not in the priority queue");

        DLNode<E> newNode = new DLNode<E>(dataItem, newValue);
        newNode.setData(dataItem);
        newNode.setValue(newValue);
}

1 个答案:

答案 0 :(得分:1)

我很确定您要做的是浏览链接列表,直到找到匹配的节点并修改该节点

DLNode<E> newNode == front;
while(newNode.getNext() != null){
    newNode = newNode.getNext();
    if(newNode.getData().equals(dataItem)){
        newNode.setValue(newValue);
        break;
    }
}