Untitled

Untitled

** Overflow 상태 : 스택이 꽉 찼을 때

 **Underflow 상태 : 완전히 비어있을 때 자료를 빼려고 할 때의 상태**

Untitled

Untitled

import java.util.Stack;

public class StackExample {

	public static void main(String[] args) {
		Stack<Integer> stack = new Stack<>();
		Stack<String> stacks = new Stack<>(); //문자열을 받을 때 사용
		
		//스택에 자료 삽입
		stack.push(1);
		stack.push(2);
		stack.push(3);
		stack.push(4);
		System.out.println("스택 내부: " + stack);
		
		//스택 맨 위 자료 삭제
		stack.pop();
		System.out.println("스택 내부: " +stack);
		
		//스택의 가장 위 데이터 반환  top
		System.out.println("스택의 가장 위 데이터: " + stack.peek());
		
		//스택에 해당 데이터가 포함되있으면 true
		System.out.println(stack.contains(2));
	
		//스택의 사이즈 출력
		System.out.println("스택의 사이즈: " + stack.size());
		
		//스택에 해당하는 숫자가 몇 번째인지 알려줌
		System.out.println(stack.search(3));
		
		//스택이 비었는지 확인해서 비었으면 true
		System.out.println(stack.empty());
		//스택을 비우는 메서드
		stack.clear();
		
		System.out.println(stack);
		
	}

}

스택 내부: [1, 2, 3, 4]
스택 내부: [1, 2, 3]
스택의 가장 위 데이터: 3
true
스택의 사이즈: 3
1
false
[]

Untitled

Untitled

Untitled