gpt4 book ai didi

java - DoubleLinkedList 从节点获取数据

转载 作者:太空宇宙 更新时间:2023-11-04 06:33:40 24 4
gpt4 key购买 nike

我创建了一个双向链表,并且尝试从链表调用 getData 方法。但是它不起作用。我正在尝试从节点获取此信息。这是来自节点的代码。

    private class Node<AnyType> 
{

AnyType data;
Node<AnyType> next;
Node<AnyType> previous;

//Creates the Node with the parameters of data next and previous
public Node(AnyType data,Node<AnyType> next, Node<AnyType> previous )
{

this.data = data;
this.next = next;
this.previous = previous;

}

//Getters and setters for data next and previous
public AnyType getData() {
return data;
}

public Node<AnyType> getNext() {
return next;
}

public Node<AnyType> getPrevious() {
return previous;
}

public void setData(AnyType data) {
this.data = data;
}

public void setNext(Node<AnyType> next) {
this.next = next;
}

public void setPrevious(Node<AnyType> previous) {
this.previous = previous;
}
}

它说 setData(AnyType data) 从未在本地使用,这可能是一个问题,但我对此不确定。

现在我尝试使用 getData 方法。这是在动画方法中,

    if (USE_LINKED_LIST)
{
for (int i = 0; i < this.linked_list.size(); i++)
{
Movable current = this.linked_list.getData();
current.move(frame_rate_duration);

if(current.dead())
{
this.linked_list.remove(current);
i--;
}

}
}

this.linked_list.getData() 给我一个错误,说我必须在 DoublyLinkedList 中创建方法 getData()。我确信这只是一个简单的错误,但任何事情都有帮助!谢谢!

这是整个 LinkedList 类

package Our_Fireworks;

/**
*
* @author Ben Hammond
*
* @param <AnyType>
*/


public class DoublyLinkedList <AnyType>
{
private Node<AnyType> header;
private Node<AnyType> footer;

public DoublyLinkedList()
{
//Creates the header Node with data set to null, next set to footer, previous set to null
header = new Node<AnyType>(null, footer, null);

footer = new Node<AnyType>(null, null, header);


}

// Creates the insert method used to insert a Node into the linked list
public void insert(AnyType data)
{
//Creates a new node to insert before the footer
Node<AnyType> newNode = new Node<AnyType>(data, footer, footer.previous);
//Sets the node previous to footer, to link to the new Node
footer.previous.setNext(newNode);
//Sets the footer node to be linked to the new Node
footer.setPrevious(newNode);
}

//Remove method to remove a Node from the linked list
public void remove (AnyType data)
{
//Starts the iteratorLooper from the first Node in the list
Iterator<AnyType> iteratorLooper = first();

//Runs the while loop as long as valid = true
while(iteratorLooper.valid())
{
//Once you receive the correct data, the loop will stop
if(iteratorLooper.getData().equals(data))
{
break;
}
//Goes to the next data member
iteratorLooper.next();
}

//Once the while loop breaks, it will delete that data member
iteratorLooper.remove();

}

//Creates the size method
public int size()
{
//Creates an int variable
int count = 0;
//Starts the iteratorLooper at the first Node
Iterator<AnyType> iteratorLooper = first();

//As long as valid returns true the while loop will run
while(iteratorLooper.valid())
{
//Will add to the count variable
count++;
//Goes to the next Node
iteratorLooper.next();
}
//Returns the count once the while loop is complete
return count;

}

//Creates the first method
public Iterator<AnyType> first()
{
//Creates a new Iterator, at header.next
Iterator<AnyType> newIterator = new Iterator<AnyType>(header.next);
//Returns the Iterator
return newIterator;
}
//Creates the last method
public Iterator<AnyType> last()
{
//Creates a new Iterator at footer.previous
Iterator<AnyType> newIterator = new Iterator<AnyType>(footer.previous);
//Returns the Iterator
return newIterator;
}

//Iterator find method
public Iterator<AnyType> find(AnyType data)
{

Iterator<AnyType> iteratorLooper = first();
//As long as valid returns true the while loop runs
while(iteratorLooper.valid())
{
//runs the loop until data is equal to "getData"
if(iteratorLooper.getData().equals(data))
{
break;
}

iteratorLooper.next();
}

//Returns iteratorLooper
return iteratorLooper;




}

//Creates the Node class
private class Node<AnyType>
{

AnyType data;
Node<AnyType> next;
Node<AnyType> previous;

//Creates the Node with the parameters of data next and previous
public Node(AnyType data,Node<AnyType> next, Node<AnyType> previous )
{

this.data = data;
this.next = next;
this.previous = previous;

}

//Getters and setters for data next and previous
public AnyType getData() {
return data;
}

public Node<AnyType> getNext() {
return next;
}

public Node<AnyType> getPrevious() {
return previous;
}

public void setData(AnyType data) {
this.data = data;
}

public void setNext(Node<AnyType> next) {
this.next = next;
}

public void setPrevious(Node<AnyType> previous) {
this.previous = previous;
}
}


//Creates the Iterator class
public class Iterator<AnyType>
{
//Creates a new node of currentNode
private Node<AnyType> currentNode;

public Iterator(Node<AnyType> currentNode)
{
this.currentNode = currentNode;
}

//Creates the valid method
public boolean valid()
{
//Checks to see if current node is not equal to the header footer, or null
if (currentNode != header && currentNode != footer && currentNode != null)
{
//If the statement is true it returns true
return true;

}
else
{
//If it is not true... it simply returns false
return false;
}

}
//Creates the next method
public void next()
{
//Checks if the next Node is not equal to null
if(currentNode.getNext() != null)
{
//Gets the next node, of what ever the current node is
currentNode = currentNode.getNext();
}
}
//Creates the previous method
public void previous()
{
//Checks if the previous Node is not equal to null
if(currentNode.getPrevious() != null)
{
//Gets the previous node of currentNode
currentNode = currentNode.getPrevious();
}
}

public AnyType getData()
{
//Gets the data inside the currentNode
return currentNode.getData();
}

//Creates the remove method
public void remove()
{
//As long as valid returns true than the if statement will run
if(valid())
{
currentNode.getPrevious().setNext(currentNode.getNext());

currentNode.getNext().setPrevious(currentNode.getPrevious());
currentNode = currentNode.getPrevious();
}


}

//Creates the insert method with the parameters of AnyType and data
public void insert(AnyType data)
{
//Creates a newNode to be inserted after currentNode
Node<AnyType> newNode = new Node<AnyType>(data, currentNode.next, currentNode );


currentNode.getNext().setPrevious(newNode);
currentNode.setNext(newNode);

}

}










}

最佳答案

您有 NodegetData() 方法,而不是您的链表类。我认为你的意思是这样的

Movable current = this.linked_list.get(i).getData();

(假设你的链表类有索引的 getter)

通常链接列表没有随机访问 getter,因此很可能整个代码应该以不同的方式编写:

for (Node<Movable> node = linked_list.getHead(); node != null; node = node.getNext()) {
Movable current = node.getData();
...
}

编辑:所以你的迭代器中有getData():

for (Iterator<Movable> iter = linked_list.first(); iter.valid(); iter.next()) {
Movable current = iter.getData();
...
}

关于java - DoubleLinkedList 从节点获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25730843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com