glog : cupucharm

[Java] Stack 본문

java/자료구조

[Java] Stack

오이호박참외 2023. 2. 3. 11:16

LIFO 형태 : 나중에 들어간 것이 먼저 나오는 구조 (후입선출)

 

 

특징

  1. 시스템 해킹에서 버퍼 오버플로우 취약점을 이용한 공격을 할 때 스택 메모리의 영역에서 함
  2. 인터럽트 처리, 수식의 계산, 서브루틴의 복귀 번지 저장 등에 쓰임
  3. 그래프의 깊이 우선 탐색(DFS)에서 사용
  4. 재귀적 함수를 호출할 때 사용

Stack 사용법

Stack 선언

import java.util.Stack;

Stack<Integer> stack = new Stack<>();
Stack<String> stack = new Stack<>();

Stack 값 추가

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

push(value) 메소드 사용

Stack 값 삭제

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.pop();   //stack에 값 제거
stack.clear(); //stack의 전체 값 제거 (초기화)

pop() 메서드 사용

Stack의 가장 상단의 값 출력

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.peek();  //stack의 가장 상단의 값 출력

peek() 메서드 이용

Stack의 기타 메서드

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);

stack.size();      //stack의 크기 출력 : 2
stack.empty();     //stack이 비어있는지 (비어있다면 true)
stack.contains(1); //stack에 1이 있는지 (있다면 true)

특징

  1. 시스템 해킹에서 버퍼 오버플로우 취약점을 이용한 공격을 할 때 스택 메모리의 영역에서 함
  2. 인터럽트 처리, 수식의 계산, 서브루틴의 복귀 번지 저장 등에 쓰임
  3. 그래프의 깊이 우선 탐색(DFS)에서 사용
  4. 재귀적 함수를 호출할 때 사용

Stack 사용법

Stack 선언

import java.util.Stack;

Stack<Integer> stack = new Stack<>();
Stack<String> stack = new Stack<>();

Stack 값 추가

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

push(value) 메소드 사용

Stack 값 삭제

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.pop();   //stack에 값 제거
stack.clear(); //stack의 전체 값 제거 (초기화)

pop() 메서드 사용

Stack의 가장 상단의 값 출력

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.peek();  //stack의 가장 상단의 값 출력

peek() 메서드 이용

Stack의 기타 메서드

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);

stack.size();      //stack의 크기 출력 : 2
stack.empty();     //stack이 비어있는지 (비어있다면 true)
stack.contains(1); //stack에 1이 있는지 (있다면 true)

특징

  1. 시스템 해킹에서 버퍼 오버플로우 취약점을 이용한 공격을 할 때 스택 메모리의 영역에서 함
  2. 인터럽트 처리, 수식의 계산, 서브루틴의 복귀 번지 저장 등에 쓰임
  3. 그래프의 깊이 우선 탐색(DFS)에서 사용
  4. 재귀적 함수를 호출할 때 사용

Stack 사용법

Stack 선언

import java.util.Stack;

Stack<Integer> stack = new Stack<>();
Stack<String> stack = new Stack<>();

Stack 값 추가

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

push(value) 메소드 사용

Stack 값 삭제

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.pop();   //stack에 값 제거
stack.clear(); //stack의 전체 값 제거 (초기화)

pop() 메서드 사용

Stack의 가장 상단의 값 출력

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.peek();  //stack의 가장 상단의 값 출력

peek() 메서드 이용

Stack의 기타 메서드

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);

stack.size();      //stack의 크기 출력 : 2
stack.empty();     //stack이 비어있는지 (비어있다면 true)
stack.contains(1); //stack에 1이 있는지 (있다면 true)

특징

  1. 시스템 해킹에서 버퍼 오버플로우 취약점을 이용한 공격을 할 때 스택 메모리의 영역에서 함
  2. 인터럽트 처리, 수식의 계산, 서브루틴의 복귀 번지 저장 등에 쓰임
  3. 그래프의 깊이 우선 탐색(DFS)에서 사용
  4. 재귀적 함수를 호출할 때 사용

Stack 사용법

Stack 선언

import java.util.Stack;

Stack<Integer> stack = new Stack<>();
Stack<String> stack = new Stack<>();

Stack 값 추가

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

push(value) 메소드 사용

Stack 값 삭제

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.pop();   //stack에 값 제거
stack.clear(); //stack의 전체 값 제거 (초기화)

pop() 메서드 사용

Stack의 가장 상단의 값 출력

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);

stack.peek();  //stack의 가장 상단의 값 출력

peek() 메서드 이용

Stack의 기타 메서드

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);

stack.size();      //stack의 크기 출력 : 2
stack.empty();     //stack이 비어있는지 (비어있다면 true)
stack.contains(1); //stack에 1이 있는지 (있다면 true)

 

'java > 자료구조' 카테고리의 다른 글

[Java] Queue  (4) 2023.02.03