gpt4 book ai didi

java - java迭代器中的自定义链表无法迭代整个列表

转载 作者:行者123 更新时间:2023-12-01 07:57:35 25 4
gpt4 key购买 nike

我用java创建了一个链表,问题在于

    public void add(T data) 

当我尝试在列表末尾添加一些内容时,“null”被添加到列表末尾。我认为我的迭代器存在一些问题,无法找到最后一个节点。请帮忙。

public class LinkedList<T> implements Iterable<T> {
private Node<T> head;
/**
* Default constructor
*
* @param head
*/
public LinkedList() {
super();
this.head = new Node<T>(null);
}
/**
* Inserts a new node at the beginning of this list.
*/
public void addFirst(T data) {
Node<T> newNode = new Node<T>(data, head);
head = newNode;
}
public void add(T data) {
Node<T> tempNpde = head;
while (tempNpde.next != null) {
tempNpde = tempNpde.next;
}
tempNpde.next = new Node<T>(data, null);
}
/**
*
* @param head
* @return
*/
public T getNode() {
return head.data;
}
@Override
public Iterator<T> iterator() {
return new ListIterator<T>();
}
public class ListIterator<T> implements Iterator<T> {
private Node<T> currentNode;
/**
* @param currentNode
*/
public ListIterator() {
super();
this.currentNode = (Node<T>) head;
}
@Override
public boolean hasNext() {
if (currentNode != null && currentNode.next != null)
return true;
else
return false;
}
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
T node = currentNode.data;
currentNode = currentNode.next;
return node;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
// Same as using struct in C
private static class Node<T> {
private T data;
private Node<T> next;
/**
* @param data
* @param next
*/
public Node(T data, Node<T> next) {
super();
this.data = data;
this.next = next;
}
/**
* @param next
*/
public Node(Node<T> next) {
super();
this.data = null;
this.next = next;
}
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.addFirst("aaaa");
list.addFirst("bbbb");
list.add("dddd");
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}

最佳答案

正如已经说过的,最大的问题是你的 next() 没有按照你想象的那样做......试试这个:

public class LinkedList<T> implements Iterable<T> {
private Node<T> head;

/**
* Default constructor
*
* @param head
*/
public LinkedList() {
super();
this.head = null;
}

/**
* Inserts a new node at the beginning of this list.
*/
public void addFirst(T data) {
Node<T> newNode = new Node<T>(data, head);
head = newNode;
}

public void add(T data) {

if ( head == null )
{
head = new Node<T>(data, null);
return;
}

Node<T> tempNode = head;
while (tempNode.next != null) {
tempNode = tempNode.next;
}
tempNode.next = new Node<T>(data, null);
}

/**
* @param head
* @return
*/
public T getNode() {
return head.data;
}

@Override
public Iterator<T> iterator() {
return new ListIterator<T>();
}

public class ListIterator<T> implements Iterator<T> {
private Node<T> currentNode;
private Node<T> previous;

/**
* @param currentNode
*/
public ListIterator() {
super();
this.currentNode = (Node<T>) head;
this.previous = null;
}

@Override
public boolean hasNext() {
if (currentNode != null && currentNode.next != null)
return true;
else
return false;
}

@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
if ( previous == null )
{
previous = currentNode;
return previous.data;
}
T node = currentNode.data;
currentNode = currentNode.next;
return currentNode.data;
}

@Override
public void remove() {
// TODO Auto-generated method stub
}
}

// Same as using struct in C
private static class Node<T> {
private T data;
private Node<T> next;

/**
* @param data
* @param next
*/
public Node(T data, Node<T> next) {
super();
this.data = data;
this.next = next;
}

/**
* @param next
*/
public Node(Node<T> next) {
super();
this.data = null;
this.next = next;
}
}

public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.add("aaaa");
list.add("bbbb");
list.addFirst("cccc");
list.add("dddd");
list.add("eeee");
list.add("ffff");
for ( String s : list ) // same thing as using an iterator
System.out.println(s);
}
}

这是整个类(class)。这应该会为您修复功能,但如果您发现任何不满意的更改(例如将 head 更改为最初为 null 而不是具有空数据的节点),请告诉我...

关于java - java迭代器中的自定义链表无法迭代整个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123109/

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