在我的程序中,我编写了自己的 LinkedList 类。还有一个实例,列表。
要在foreach循环中使用它,LinkedList需要实现Iterable?
for(Node node : llist) {
System.out.print(node.getData() + " ");
}
以下是我的 LinkedList 类。请让我知道如何使其可迭代?
public class LinkedList implements Iterable {
private Node head = null;
private int length = 0;
public LinkedList() {
this.head = null;
this.length = 0;
}
LinkedList (Node head) {
this.head = head;
this.length = 1;
}
LinkedList (LinkedList ll) {
this.head = ll.getHead();
this.length = ll.getLength();
}
public void appendToTail(int d) {
...
}
public void appendToTail(Node node) {
...
}
public void deleteOne(int d) {
...
}
public void deleteAll(int d){
...
}
public void display() {
...
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public boolean isEmpty() {
if(this.length == 0)
return true;
return false;
}
}