Изменение значения узла универсального типа в двусвязном списке - PullRequest
2 голосов
/ 15 марта 2019

Я работаю над упражнением, в котором я хочу создать метод, который изменяет значение узла, после проверки, находится ли он в списке элементов. Я попытался сделать это, создав новый объект, 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);
}

1 Ответ

1 голос
/ 15 марта 2019

Я почти уверен, что вы хотите просмотреть список связанных ссылок, пока не найдете подходящий узел и не измените этот узел

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