在双向链表中更改泛型类型节点的值

问题描述 投票:2回答:1

我正在进行一项练习,我想在检查它是否在元素列表中之后创建一个更改节点值的方法。我试过这样做是通过创建一个新对象newNode,并使用节点类中的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);
}
java generics doubly-linked-list
1个回答
1
投票

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

DLNode<E> newNode == front;
while(newNode.getNext() != null){
    newNode = newNode.getNext();
    if(newNode.getData().equals(dataItem)){
        newNode.setValue(newValue);
        break;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.