java迭代器中的自定义链表无法遍历整个列表 [英] Custom linked list in java iterator not able to iterate over the entire list
问题描述
我在java中创建了一个链表,问题在于
I have created a linked list in java, the issue is with
public void add(T data)
当我尝试在列表末尾添加一些东西时,null被添加到结尾列表。我认为我的迭代器有一些问题,它无法找到最后一个节点。
Plz help。
when I try to add some thing at the end of the list, "null" is getting added to the end of the list. I think there is some problem with my iterator which is not able to find the last node. Plz help.
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());
}
}
推荐答案
As已经说过,最大的问题是你的 next()
没有做你想象的那样......试试这个:
As was already said, the biggest issue was that your next()
wasn't doing what you thought it was... try this:
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);
}
}
这是整个班级。这应该为您修复功能,但如果您发现任何不满意的更改(例如,将 head
更改为最初 null
而不是具有空数据的节点),请告诉我......
This is the entirety of the class. This should fix the functionality for you, but if you spot any unsatisfactory changes (e.g. changing the head
to initially be null
instead of a node with null data), let me know...
这篇关于java迭代器中的自定义链表无法遍历整个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!