我有一个 Student
对象的堆栈 s
和一个 Student
对象 e
。
如何在不使用 s.peek()
方法的情况下删除最上面出现的 e
上方的堆栈中的所有元素?
最佳答案
弹出直到遇到e
,然后再次推送e
。
Student top = null;
while(!s.isEmpty() && !e.equals(top)){
top = s.pop();
}
if(e.equals(top)) {
s.push(top);
} else {
// e was not on the stack
}
关于java - 不使用 peek() 从堆栈中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21340660/