import javax.xml.soap.Node;
interface Collection{
public void add(Object x);
public Iterator iterator();
}
interface Iterator{
public Object next();
public boolean hasNext();
}
@SuppressWarnings("serial")
class NoSuchElementException extends RuntimeException{}
class LinkedList implements Collection{
Object elt;
Node next=null;
Node(Object elt){
this.elt=elt;
}
protected Node head=null,tail=null;
public LinkedList() {}
public void add(Object alt) {
if(head==null) {
head=new Node(elt);
tail=head;
}
else {
tail.next=new Node(elt);
tail=tail.next;
}
}
public Iterator iterator() {
return new Iterator() {
protected Node ptr=head;
public boolean hasNext() {
return ptr!=null;
}
public Object next() {
if(ptr!=null) {
Object elt=ptr.elt;
ptr=ptr.next;
return elt;
}
else throw new NoSuchElementException();
}
};
}
}
Java语言-Test1.java
最新推荐文章于 2022-09-22 20:46:54 发布