使用 Java 在双向链表上进行无限迭代器循环
Endless iterator loop on doubly linked list using Java
我创建了一个双向链表并实现了我自己的迭代器。
但是,我做错了什么,我的迭代器导致无限循环。
一直在努力寻找错误,因此非常感谢任何反馈。提前致谢。我为代码墙道歉,我认为错误出在我创建 Node
class.
的迭代器中
我的代码:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyDoubleEndedLinkedList<T extends Comparable<T>> implements
Iterable<T> {
// initialising Nodes including the two sentinal nodes
private Node<T> head;
private Node<T> tail;
private Node<T> current;
private int currentsize;
MyDoubleEndedLinkedList() {
head = new Node<T>();
tail = new Node<T>();
head.setNext(tail);
tail.setPrevious(head);
current = head;
currentsize = 0;
// Methods used to help loop and iterate through the list
public boolean isEmpty() {
return (current == head && current == tail);
}
public boolean endList() {
return (current != tail);
}
public void resetCurrent() {
current = head;
}
public void nextCurrent() {
current = current.getNext();
}
public T getCurrent() {
return current.getData();
}
public int size() {
return this.currentsize;
}
@Override
public Iterator<T> iterator() {
return new LinkedListIterator<T>();
}
// Node class for doublyLinkedList
public class Node<E> {
private Node<E> previous;
private Node<E> next;
private E data;
Node() {
previous = null;
next = null;
data = null;
}
Node(Node<E> newPrevious, Node<E> newNext, E newData) {
previous = newPrevious;
next = newNext;
data = newData;
}
// set previous node
public void setPrevious(Node<E> newPrevious) {
previous = newPrevious;
}
// set Next node
public void setNext(Node<E> newNext) {
next = newNext;
}
public void setData(E newData) {
data = newData;
}
public Node<E> getPrevious() {
return previous;
}
public Node<E> getNext() {
return next;
}
public E getData() {
return data;
}
}
class LinkedListIterator<E> implements Iterator<T> {
private Node<T> current;
private Node<T> previous;
private Node<T> previous2;
private boolean removeCalled;
public LinkedListIterator() {
current = head;
previous = null;
previous2 = null;
removeCalled = false;
}
public boolean hasNext() {
return (current != null);
}
public T next() {
if (hasNext()) {
T temp = current.getData();
previous2 = previous;
previous = current;
current = current.next;
removeCalled = false;
return temp;
}
throw new NoSuchElementException();
}
public void remove() {
if (previous == null || removeCalled) {
throw new IllegalStateException();
}
if (previous2 == null) {
head = current;
} else {
previous2.setNext(current);
previous = previous2;
}
removeCalled = true;
throw new UnsupportedOperationException();
}
}}
所以我无法在您的代码中找到错误,但这是 Java 中基本链表的更简单实现。如果您向我展示如何向列表中添加元素,将更容易找到。
import java.util.Iterator;
public class MyLinkedList<T> implements Iterable<T> {
private Node head = null;
private Node tail = null;
public static void main(String[] args) {
MyLinkedList<String> li = new MyLinkedList<>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
for (String s : li) {
System.out.println(s);
}
}
public void add(T data) {
if (head == null) {
head = new Node(data, null);
tail = head;
} else {
Node n = new Node(data, tail);
tail.next = n;
tail = n;
}
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
Node current = head;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
T data = current.data;
current = current.next;
return data;
}
@Override
public void remove() {
}
};
}
class Node {
final T data;
Node prev = null;
Node next = null;
Node(T data, Node prev) {
this.data = data;
this.prev = prev;
}
}
}
我创建了一个双向链表并实现了我自己的迭代器。
但是,我做错了什么,我的迭代器导致无限循环。
一直在努力寻找错误,因此非常感谢任何反馈。提前致谢。我为代码墙道歉,我认为错误出在我创建 Node
class.
我的代码:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyDoubleEndedLinkedList<T extends Comparable<T>> implements
Iterable<T> {
// initialising Nodes including the two sentinal nodes
private Node<T> head;
private Node<T> tail;
private Node<T> current;
private int currentsize;
MyDoubleEndedLinkedList() {
head = new Node<T>();
tail = new Node<T>();
head.setNext(tail);
tail.setPrevious(head);
current = head;
currentsize = 0;
// Methods used to help loop and iterate through the list
public boolean isEmpty() {
return (current == head && current == tail);
}
public boolean endList() {
return (current != tail);
}
public void resetCurrent() {
current = head;
}
public void nextCurrent() {
current = current.getNext();
}
public T getCurrent() {
return current.getData();
}
public int size() {
return this.currentsize;
}
@Override
public Iterator<T> iterator() {
return new LinkedListIterator<T>();
}
// Node class for doublyLinkedList
public class Node<E> {
private Node<E> previous;
private Node<E> next;
private E data;
Node() {
previous = null;
next = null;
data = null;
}
Node(Node<E> newPrevious, Node<E> newNext, E newData) {
previous = newPrevious;
next = newNext;
data = newData;
}
// set previous node
public void setPrevious(Node<E> newPrevious) {
previous = newPrevious;
}
// set Next node
public void setNext(Node<E> newNext) {
next = newNext;
}
public void setData(E newData) {
data = newData;
}
public Node<E> getPrevious() {
return previous;
}
public Node<E> getNext() {
return next;
}
public E getData() {
return data;
}
}
class LinkedListIterator<E> implements Iterator<T> {
private Node<T> current;
private Node<T> previous;
private Node<T> previous2;
private boolean removeCalled;
public LinkedListIterator() {
current = head;
previous = null;
previous2 = null;
removeCalled = false;
}
public boolean hasNext() {
return (current != null);
}
public T next() {
if (hasNext()) {
T temp = current.getData();
previous2 = previous;
previous = current;
current = current.next;
removeCalled = false;
return temp;
}
throw new NoSuchElementException();
}
public void remove() {
if (previous == null || removeCalled) {
throw new IllegalStateException();
}
if (previous2 == null) {
head = current;
} else {
previous2.setNext(current);
previous = previous2;
}
removeCalled = true;
throw new UnsupportedOperationException();
}
}}
所以我无法在您的代码中找到错误,但这是 Java 中基本链表的更简单实现。如果您向我展示如何向列表中添加元素,将更容易找到。
import java.util.Iterator;
public class MyLinkedList<T> implements Iterable<T> {
private Node head = null;
private Node tail = null;
public static void main(String[] args) {
MyLinkedList<String> li = new MyLinkedList<>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
for (String s : li) {
System.out.println(s);
}
}
public void add(T data) {
if (head == null) {
head = new Node(data, null);
tail = head;
} else {
Node n = new Node(data, tail);
tail.next = n;
tail = n;
}
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
Node current = head;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
T data = current.data;
current = current.next;
return data;
}
@Override
public void remove() {
}
};
}
class Node {
final T data;
Node prev = null;
Node next = null;
Node(T data, Node prev) {
this.data = data;
this.prev = prev;
}
}
}