import java.util.Stack;
public class StackLastinFirstout {
public static void main(String[] args) {
Stack s = new Stack();
s.push("A");
s.push("B");
s.push("C");
System.out.println(s);
System.out.println(s.pop());//移除堆栈顶部的对象,并作为此函数的值返回该对象-最后一项 C
System.out.println("Next: " + s.peek());//查看堆栈顶部的对象,但不从堆栈中移除它。最后一项 C
s.push("E");
int count = s.search("E");//返回对象在堆栈中的位置,以 1 为基数。
while(count!=-1&&count>1){
s.pop();
count--;
}
System.out.println(s);
}
}
9. 13. 6. Stack基本用法 To find out if an element is on the stack: the search() method
本文通过一个简单的Java程序展示了栈的基本操作,包括元素的压入、弹出及查找等。通过实例说明了栈的数据结构特点——后进先出(LIFO)。

被折叠的 条评论
为什么被折叠?



