这个题比较简单,由于栈是后入先出,所以需要两个栈:
static void copyStack()
{
LinkStack<String> stack = new LinkStack<>();
stack.push("a");
stack.push("b");
stack.push("c");
LinkStack<String> s1 = new LinkStack<>();
LinkStack<String> s2 = new LinkStack<>();
Iterator<String> it = stack.iterator();
while (it.hasNext())
{
s1.push(it.next());
}
it = s1.iterator();
while (it.hasNext())
{
s2.push(it.next());
}
System.out.println("输出s2: ");
while (!s2.isEmpty())
{
System.out.println(s2.pop());
}
}