Update the chapter of stack and queue.

This commit is contained in:
krahets
2023-03-12 04:14:36 +08:00
parent 050b922f8a
commit 82bbdd444d
5 changed files with 27 additions and 35 deletions

View File

@ -11,23 +11,22 @@ import java.util.*;
public class stack {
public static void main(String[] args) {
/* 初始化栈 */
// 在 Java 中,推荐将 ArrayList 当作栈来使用
List<Integer> stack = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
/* 元素入栈 */
stack.add(1);
stack.add(3);
stack.add(2);
stack.add(5);
stack.add(4);
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("栈 stack = " + stack);
/* 访问栈顶元素 */
int peek = stack.get(stack.size() - 1);
int peek = stack.peek();
System.out.println("栈顶元素 peek = " + peek);
/* 元素出栈 */
int pop = stack.remove(stack.size() - 1);
int pop = stack.pop();
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + stack);
/* 获取栈的长度 */