[英]Changing value of a generic type node in a doubly linked list
I'm working on an exercise where I want to make a method that changes the value of a node, after checking if it's in the list of elements.我正在做一个练习,在检查它是否在元素列表中之后,我想制作一个更改节点值的方法。 I've tried doing this by creating a new object, newNode, and using the setter methods from the node class to change the value, but I'm not getting anywhere.
我已经尝试通过创建一个新对象 newNode 并使用节点类中的 setter 方法来更改值来做到这一点,但我一无所获。 How should I approach this problem in order to understand it better.
我应该如何处理这个问题以便更好地理解它。 Thanks.
谢谢。
Linked List Class:链表类:
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);
}
I'm pretty sure what you want to do is to go through your linked list until you find a matching node and modify that node我很确定您想要做的是遍历您的链表,直到找到匹配的节点并修改该节点
DLNode<E> newNode == front;
while(newNode.getNext() != null){
newNode = newNode.getNext();
if(newNode.getData().equals(dataItem)){
newNode.setValue(newValue);
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.