The student at the top of the stack is Gullion,Hailey
Student Mcglothlen,Shizue is removed from the stack
Here are all the elements of the Stack using an Iterator
--------------------------------------------------------
Stack$Node@3012db7c
Stack$Node@2607c28c
Stack$Node@477588d5
Stack$Node@756a7c99
Stack$Node@221a5d08
Stack$Node@70d1c9b5
Stack$Node@5d11c3f0
Stack$Node@3956f14c
Stack$Node@7afbd1fc
null
Here are all the elements in the Stack
--------------------------------------
Putney,John
Larkey,Ismael
Winkler,Isiah
Aceto,Liana
Hamill,Crissy
Caraway,Elmira
Gullion,Hailey
Rodrigez,Jonie
Madruga,Terrell
Williams,Diego
使用 Iterator 的 Stack 的第一个元素列表显然不起作用。我不知道为什么。这是我的 Stack 类中的 Iterator 代码:
public Iterator<Student> iterator() { return new ListIterator(); }
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator<Student> {
private Node<Student> current = top;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
@SuppressWarnings("unchecked")
public Student next() {
if (!hasNext()) throw new NoSuchElementException();
current = current.next;
return (Student)current;
}
}
这是我的 Driver 类中似乎有问题的代码:
System.out.println("\nHere are all the elements of the Stack using an Iterator");
System.out.println("--------------------------------------------------------");
Iterator <Student> iter = myStack.iterator();
while (iter.hasNext() )
System.out.println(iter.next() );
以下是所有课程:
堆栈: http: //pastebin.com/2HVLVHuM
队列类: http: //pastebin.com/3q537kHW
学生班: http: //pastebin.com/UnBB7kPA
驱动类: http: //pastebin.com/yeA34MNd
我只能在堆栈类中编写代码。这样做的目的是使用队列实现堆栈。希望这可以帮助