BE/Java

Stack 이란?

jjuni_96 2024. 2. 13. 10:58
728x90

구조

FILO(First In Last Out) 구조로 처음 삽입된 데이터가 가장 마지막으로 추출

 

 

특징

1. 시스템 해킹에서 버퍼 오버플로우  취약점을 이용한 공격을 할 때 스택메모리의 영역에서 사용

2. 인터럽트 처리, 수식의 연산, 서브루틴의 복귀 주소 저장 등에 쓰임

3. 그래프 깊이 탐색(BFS)에서 사용

4. 재귀함수를 호출할때 사용

 

 

Stack 생성

import java.util.Stack; // Import

// Int형
Stack<Integer> stackInt = new Stack<>();
// String형
Stack<String> stackString = new Stack<>();
// Boolean형
Stack<Boolean> stackBoolean = new Stack<>();

 

데이터 삽입

stackInt.push(5);
stackInt.push(4);
stackInt.push(3);
stackInt.push(2);
stackInt.push(1);
System.out.println(stackInt);

-- [5, 4, 3, 2, 1]

 

 

데이터 삭제

stackInt.pop();
stackInt.pop();
stackInt.pop();

System.out.println(stackInt);

-- [5, 4]

 

 

BFS 탐색은 추후에..

728x90
반응형
LIST