package Day0129;
/*
* @Author jingxurun
* @Date 2024/1/29 星期一 16:37
* @Version
**/
import java.util.Stack;
public class StackTest {
public static void main(String[] args) {
// 创建一个新的栈
Stack<Integer> stack = new Stack<>();
// 将元素入栈
stack.push(11);
stack.push(22);
stack.push(33);
stack.push(44);
stack.push(55);
// 打印栈的内容
System.out.println("Stack contents:");
for (int item : stack) {
System.out.println(item);
}
// 查看并打印栈顶元素
System.out.println("Top element: " + stack.peek());
// 依次出栈并打印
while (!stack.isEmpty()) {
System.out.println("Popped: " + stack.pop());
}
}
}