Stack

이규은·2021년 9월 27일
0

자료구조

목록 보기
2/3

Stack은 상자에 물건을 쌓아 올리듯이 데이터를 쌓는 자료구조이다. Stack은 나중에 들어간 것이 먼저 나오는 Last In First Out의 형태를 띈다.

Stack 선언

Stack<Integer> stack1 = new Stack<>();	//int형
Stack<String> stack2 = new Stack<>();	//char형

Stack 값 추가

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

stack.push(1);
stack.push(2);
stack.push(3);

push()를 이용하여 값을 추가할 수 있다.

Stack 값 삭제

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

stack.push(1);
stack.push(2);
stack.push(3);	//값 추가

stack.pop();	//가장 마지막에 넣은 값을 제거
stack.clear();	//모든 값을 제거
profile
안녕하세요

0개의 댓글