티스토리 뷰
Introduction
Queue data structure follows First In First Out (FIFO) order. Not like Stack data structure, Queue allows the least recently added value to be freed the earliest. Thus, Queue data structure is more in accordance with common sense in reality familiar to us.
Implementation
Since Queue takes out the value pushed the earliest above all, two pointers are required for tracking the working principle. These pointers are called rear() and front(). When inserting a new value to a queue, namely a behaviour "Enqueue", only rear() of a queue is referred and changed. On the other hand, for "Dequeue", which is as expected a deletion of a value from a queue, front() of a queue is always modified. This is how Queue data structure behaves not only on Java but in general.
Methods
Some of the methods with which Java Queue class provides include the following:
1. offer(value) / add(value)
import java.util.Queue;
import java.util.LinkedList;
public class QueueDataStructure {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(2);
queue.add(5);
System.out.println(queue);
}
}
code block 1-1: Code snippets that add integer values 2 and 5 to Queue data structure and print.
As shown above, LinkedList class is utilised to initalise Queue. Both offer() and add() introduce a new element to a structure (Enqueue), add() throws exception to confirm the failure of insertion while offer() returns boolean value false. The output of code snippets above is as below:
[2, 5]
2. remove() / poll()
import java.util.Queue;
import java.util.LinkedList;
public class QueueDataStructure {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
int element1 = 0;
int element2 = 0;
queue.offer(2);
queue.add(5);
element1 = queue.remove();
element2 = queue.poll();
System.out.println(queue);
System.out.println();
System.out.println("Element erased: " + element1);
System.out.println();
System.out.println("Element erased: " + element2);
}
}
code block 1-2: Code snippets that delete integer values 5 and 2 from Queue data structure and print a state of Queue and a value just deleted using poll().
Either remove() or poll() may be a method that dequeues data value from Queue. Since Queue only allows the value to be deleted from "front()", no parameter is required for the methods. Both behave similarly, while the returned values are distinct. poll() returns null when Queue.isEmpty() is true, and remove() throws exception. The code snippets above outputs as below, here we can observe that queue was dequeued in a same order it has been enqueued:
[]
Element erased: 2
Element erased: 5
Conclusion
Queue data structure follows FIFO order, thus more appropriate in a situation where early birds take advantages. Not like Stack, there are two methods each for inserting and deleting values from structure.
Queue is useful in:
1. Realising Breadth first search algorithm
2. Computer buffer for lining up waiting inputs
'Algorithms' 카테고리의 다른 글
[알고리즘] Algorithms Study with JAVA - Backtracking (0) | 2022.11.18 |
---|---|
[알고리즘] Quick Sort: 구현 (Array 2단계) (0) | 2022.11.17 |
[알고리즘] Quick Sort 이론 및 구현: List (0) | 2022.11.17 |
[알고리즘] Quick Sort: 이론 (Array 1단계) (0) | 2022.11.16 |
[알고리즘] JAVA programming language - 1: Stack data structure (0) | 2022.07.14 |
- Total
- Today
- Yesterday
- 실시간데이터
- Java Data Types
- @RequestBody
- 지연 로딩
- ci/cd
- json web token
- Jackson
- 알고리즘
- DeSerialization
- spring
- JPA
- Firebase
- JOIN FETCH
- google cloud
- FCM
- 코테
- Spring Boot
- N+1
- 기지국 설치
- 가상 서버
- JPQL
- docker
- LazyInitializationException
- 깃랩
- DTO
- 프로그래머스
- gitlab
- 역직렬화
- 인증/인가
- 도커
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |