
출처 : https://m.hanbit.co.kr/channel/category/category_view.html?cms_code=CMS8073601837
int[] array = new int[5];
array[0] = 10;
array[1] = 20;
List<String> arrayList = new ArrayList<>();
arrayList.add("Hello");
arrayList.add("World");
List<String> linkedList = new LinkedList<>();
linkedList.add("Hello");
linkedList.add("World");
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
int top = stack.pop(); // top = 2
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
int front = queue.poll(); // front = 1
Queue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add(2);
priorityQueue.add(1);
int priorityFront = priorityQueue.poll(); // priorityFront = 1
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
int value = map.get("one"); // value = 1
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("apple"); // 중복된 요소는 추가되지 않습니다.
class Node {
int value;
Node left, right;
public Node(int value) {
this.value = value;
left = right = null;
}
}
class BinaryTree {
Node root;
void add(int value) {
root = addRecursive(root, value);
}
Node addRecursive(Node current, int value) {
if (current == null) {
return new Node(value);
}
if (value < current.value) {
current.left = addRecursive(current.left, value);
} else if (value > current.value) {
current.right = addRecursive(current.right, value);
}
return current;
}
}
class Graph {
private Map<Integer, List<Integer>> adjVertices;
public Graph() {
adjVertices = new HashMap<>();
}
void addVertex(int label) {
adjVertices.putIfAbsent(label, new ArrayList<>());
}
void addEdge(int v1, int v2) {
adjVertices.get(v1).add(v2);
adjVertices.get(v2).add(v1); // 무방향 그래프일 경우
}
List<Integer> getAdjVertices(int label) {
return adjVertices.get(label);
}
}
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
// Deque 선언 및 초기화
Deque<Integer> deque = new ArrayDeque<>();
// 요소 추가 - 앞쪽과 뒤쪽 모두 가능
deque.addFirst(10); // 앞쪽에 추가
deque.addLast(20); // 뒤쪽에 추가
deque.offerFirst(5); // 앞쪽에 추가
deque.offerLast(25); // 뒤쪽에 추가
// 현재 Deque 상태 출력
System.out.println("Deque: " + deque); // 출력: Deque: [5, 10, 20, 25]
// 요소 제거 - 앞쪽과 뒤쪽 모두 가능
int firstElement = deque.removeFirst(); // 앞쪽 요소 제거
int lastElement = deque.removeLast(); // 뒤쪽 요소 제거
// 제거한 요소 출력
System.out.println("Removed First Element: " + firstElement); // 출력: Removed First Element: 5
System.out.println("Removed Last Element: " + lastElement); // 출력: Removed Last Element: 25
// 현재 Deque 상태 출력
System.out.println("Deque after removals: " + deque); // 출력: Deque after removals: [10, 20]
// 요소 조회 - 제거하지 않고 조회
int peekFirst = deque.peekFirst(); // 앞쪽 요소 조회
int peekLast = deque.peekLast(); // 뒤쪽 요소 조회
// 조회한 요소 출력
System.out.println("Peek First Element: " + peekFirst); // 출력: Peek First Element: 10
System.out.println("Peek Last Element: " + peekLast); // 출력: Peek Last Element: 20
// 요소 삽입 - 예외를 발생시키지 않는 방법
deque.offerFirst(15); // 앞쪽에 추가
deque.offerLast(30); // 뒤쪽에 추가
// 현재 Deque 상태 출력
System.out.println("Deque after offers: " + deque); // 출력: Deque after offers: [15, 10, 20, 30]
// Deque가 비어 있는지 확인
boolean isEmpty = deque.isEmpty();
System.out.println("Is Deque empty? " + isEmpty); // 출력: Is Deque empty? false
}
}
각 자료 구조는 특정한 문제를 해결하기 위해 설계되었으며, 상황에 따라 적절한 자료 구조를 선택하는 것이 중요하다!