[英]peekBottom method in stack by singly linked list
我已经完成了peekTop,这是可靠的peek功能吗? 窥视底部有什么区别?
我如何获得最后一个元素?
public E PeekTop() {
if (isEmpty())
System.out.print("stack is empty ");
return top.getElement();
如jtahlborn所述 ,如果您的堆栈是通过单个链表实现的,则需要遍历整个链表才能检索堆栈底部的项目-即:
public E PeekBottom() {
if (isEmpty()) {
System.err.println("Stack is empty - no bottom");
return null;
}
E stackItem = top;
while (stackItem.hasNext())
stackItem = stackItem.next();
return stackItem.getElement();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.