此方法应该返回当前堆栈的副本,其中项目反转。
public LinkedStack<E> reversed()
{
LinkedStack<E> that= new LinkedStack<E>();
if(this.isEmpty()==true){
return this;
}
else{
while(this.isEmpty())//changed from this.isEmpty()==true
{
Node<E> snode=this.top;
that.push(snode.getData());
this.pop();
snode=snode.getLink();
/*
that.push(pop()); works perfectly
*/
}
return that;
}
}
更新
好的答案之一似乎让我更接近解决方案。它有效,但仅适用于方法中创建的堆栈。我遇到的问题是将它链接到这个堆栈,以便我可以返回堆栈的副本this
。我正在使用链接堆栈。