μ€νμ μ¬μ μ μλ―Έλ βμλ€βμ λλ€. μμ¬μλ λ°μ€μ κ°μ΄ 차곑차곑 μμ μ¬λ¦° ννμ ꡬ쑰λ₯Ό μ€ν(stack)μ΄λΌ λΆλ¦ λλ€. μ΄λ¬ν μ€νμ κ°λ μ μΆμννμ¬ μλ£κ΅¬μ‘°λ‘ λ§λ κ²μ΄ μ€ν μλ£κ΅¬μ‘°μ λλ€.
public class ArrayStack {
int top; //μΈλ±μ€
int size; //μ€ν λ°°μ΄μ ν¬κΈ°
int [] stack;
public ArrayStack(int size) {
this.size = size;
stack = new int[size];
top = -1;
}
public void push(int item) {
stack[++top] = item;
System.out.println(stack[top] + " Push!");
}
public void pop() {
System.out.println(stack[top] + " Pop!");
int pop = stack[top];
stack[top--] = 0;
}
public void peek() {
System.out.println(stack[top] + " Peek!");
}
}
Stack<Integer> stack = new Stack<>(); //intν μ€ν μ μΈ
stack.push(1); // stackμ κ° 1 μΆκ°
stack.push(2); // stackμ κ° 2 μΆκ°
stack.push(3); // stackμ κ° 3 μΆκ°
stack.peek(); // stackμ κ°μ₯ μλ¨μ κ° μΆλ ₯
stack.size(); // stackμ ν¬κΈ° μΆλ ₯ : 2
stack.empty(); // stackμ΄ λΉμ΄μλμ check (λΉμ΄μλ€λ©΄ true)
stack.contains(1) // stackμ 1μ΄ μλμ§ check (μλ€λ©΄ true)
stack.pop(); // stackμ κ° μ κ±°
stack.clear(); // stackμ μ 체 κ° μ κ±° (μ΄κΈ°ν)
νΌλλ°± λ° κ°μ μ μ λκΈμ ν΅ν΄ μλ €μ£ΌμΈμπ