Queue(큐)
- FIFO (First In First Out) 형태를 가지는 자료구조.
- 데이터가 입력되는 순서대로 처리되어야 하는 상황에서 사용.
코드
/* LinkedList로 구현한 Queue */
import java.util.LinkedList;
// Queue ADT
public interface Queue<E> {
public void enqueue(E e);
public E dequeue();
public int size();
}
class LinkedListQueue<E> implements Queue<E> {
LinkedList<E> queue;
public LinkedListQueue() {
this.queue = new LinkedList<E>();
}
public void enqueue(E e) {
this.queue.add(e);
}
public E dequeue() {
E e = this.queue.getFirst();
this.queue.removeFirst();
return e;
}
public int size() {
return this.queue.size();
}
}
class Test3 {
public static void main(String[] args) {
LinkedListQueue<String> queue = new LinkedListQueue<>();
queue.enqueue("A");
queue.enqueue("B");
queue.enqueue("C");
System.out.println("Size: " + queue.size());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
}
}
실행 결과
Size: 3
A
B
C
2024.09.02 - [Java] - [Java] 14. Collections Framework
[Java] 14. Collections Framework
Chapter 1: Collection FrameworkCollection 개요Java Collections FrameworkCollection 클래스의 저장 구조Java Collections Framework 구성Collection 인터페이스Collection 인터페이스의 주요 메소드 Chapter 2: Iterator, Comparable, Compa
lightningtech.tistory.com
출처: https://github.com/gikpreet/class-programming_with_java/tree/master
GitHub - gikpreet/class-programming_with_java
Contribute to gikpreet/class-programming_with_java development by creating an account on GitHub.
github.com
'자료구조' 카테고리의 다른 글
[자료구조] Binary Search Tree (BST, 이진 탐색 트리) (0) | 2024.09.07 |
---|---|
[자료구조] Hash Table (해시 테이블) (0) | 2024.09.07 |
[자료구조] LinkedList (0) | 2024.09.04 |
[Java] ArrayList (0) | 2024.09.02 |
[자료구조] 스택(Stack) with Java (0) | 2024.09.02 |