Algorithm
스택 (Stack)
LearnerKSH
2022. 3. 7. 19:09
728x90
반응형
스택
1) 선입후출 : 먼저 들어온 데이터가 나중에 나가는 형식

예제코드
import java.util.Stack;
/**
* Stack
*/
public class M1_Stack {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(5); // push 5
stack.push(3); // push 3
stack.push(8); // push 8
stack.pop(); // pop 8
stack.push(2); // push 2
stack.pop(); // pop 2
while(!stack.isEmpty()) {
System.out.println(stack.peek()); // 최상위 데이터 peek
stack.pop(); // pop
}
}
}
stack.push(5)
| 5 |
stack.push(3)
| 5 | 3 |
stack.push(8)
| 5 | 3 | 8 |
stack.pop()
| 3 | 8 |
stack.push(2)
| 3 | 8 | 2 |
stack.pop()
| 8 | 2 |
반응형