본문 바로가기
Algorithm/기타

스택과 큐

by 당진개발자 2024. 1. 17.

1. 스택

- 삽입과 삭제 연산이 후입선출로 이뤄지는 자료구조

 

 


2. 큐

- 삽입과 삭제 연산이 선입선출로 이뤄지는 자료구조

 


 

3. 스택과 큐 코드 구현

public class Example {
	public static void main(String[] args) {
		Stack st = new Stack();
		Queue q = new LinkedList();	//Queue의 인터페이스 구현체인 LinkedList를 사용
		
		st.push("0");
		st.push("1");
		st.push("2");
		
		q.offer("0");
		q.offer("1");
		q.offer("2");
		
		System.out.println("=== Stack ===");
		while(!st.isEmpty()) {
			System.out.println(st.pop());
		}
		
		System.out.println("=== Queue ===");
		while(!q.isEmpty()) {
			System.out.println(q.poll());
		}
	}
}

'Algorithm > 기타' 카테고리의 다른 글

DFS / BFS  (0) 2024.01.22
투 포인터  (1) 2024.01.16
구간 합  (0) 2024.01.15
배열과 리스트  (0) 2024.01.15
코딩테스트 준비하기  (0) 2024.01.08