High Java/Collection FrameWork
[Java] Stack & Queue
Fizzyelf
2020. 9. 26. 15:13
1. Stack
- 후입선출(LIFO, Last In First Out)의 자료구조
LinkedList<String > stack = new LinkedList<>();
stack.push("홍길동");
stack.push("일지매");
stack.push("변학도");
stack.push("강감찬");
System.out.println("현재 stack값들 : " + stack);
String data = stack.pop();
System.out.println("꺼내온 자료 : " + data);
System.out.println("현재 stack 값들 : " +stack);
System.out.println("꺼내온 자료 : " + stack.pop());
String top = stack.peek();
System.out.println("Top에 있는 데이터 값 : "+top);
stack.push("성춘향");
System.out.println("현재 stack 값들 : " +stack);
System.out.println("꺼내온 자료 : " + stack.pop());
System.out.println("현재 stack 값들 : " +stack);
if(stack.isEmpty()) {
System.out.println("비어있어요");
} else {
System.out.println("데이터가 있어요");
}
- push() : 데이터를 삽입
- pop() : 데이터를 제거
- peek() : 스택 구조에서 가장 위에 있는 데이터를 반환
- isEmpty() : 데이터가 비어있을 때 true 반환, 아니면 false
2. Queue
- 선입선출(FIFO, First In First Out)의 자료구조
LinkedList<String> queue = new LinkedList<>();
queue.offer("홍길동");
queue.offer("일미재");
queue.offer("변학도");
queue.offer("강감찬");
System.out.println("현재 queue의 값 : " + queue);
String temp = queue.poll();
System.out.println("꺼내온 자료 : " + temp);
System.out.println("꺼내온 자료 : " + queue.poll());
System.out.println("현재 queue의 값 : " + queue);
if(queue.offer("성춘향")) {
System.out.println("신규 등록 자료 : 성춘향");
}
System.out.println("현재 queue의 값 : " + queue);
System.out.println("꺼내온 자료 : " + queue.poll());
- add() : 데이터 삽입
- remove() : 데이터 삭제
- peek() : 큐 구조에서 가장 위에 있는 데이터 반환
- isEmpty() : 데이터가 비어있을 때 true 반환, 아니면 false