티스토리 뷰
[알고리즘] JAVA programming language - 1: Stack data structure
Nickolodeon 2022. 7. 14. 22:42Introduction
One of the most famous data structures used in JAVA is Stack.
Stack follows "LIFO (Last In First Out)" order, meaning a data value pushed on stack firstly is popped with priority. This rule Stack data structure follows may be unfamiliar to us, as we are more used to the concept of lining up. In the formation of lining up, the person who walked into a line earlier would enter into the place whatever the line is for (e.g. restaurant) faster. However, in Stack data structure, even though the person arrived earlier, they could not take temporal advantage in entering over others.
Implementation
Stack in JAVA uses the keyword 'Stack', and declared in a similar sense with any other data structures in JAVA (e.g. Arrays, ArrayList, etc.). Below is how an empty stack variable is declared using JAVA.
import java.util.Stack;
public class StackExample {
public void createStack() {
Stack<String> strStack = new Stack<>();
}
}
code block 1-1: Code snippets that create an empty Stack taking as elements String type values.
Now, as we have already created an empty Stack, all we are to do is to enjoy every various methods that are allowed with Stack data structure!
Methods in Stack data structure
1. push(data_type data_value)
import java.util.Stack;
public class StackExample {
public void createStack() {
Stack<String> strStack = new Stack<>();
strStack.push("Element 1");
strStack.push("Element 2");
strStack.push("Element 3");
System.out.println(strStack);
}
public static void main(String args[]) {
StackExample test = new StackExample();
test.createStack();
}
}
code block 1-2: Code snippets that add values "Element 1", "Element 2", "Element 3" to Stack data structure and print.
The code above adds three elements each using 'push' method. Output of the above code is like below:
[Element 1, Element 2, Element 3]
2. pop()
import java.util.Stack;
public class StackExample {
public void createStack() {
Stack<String> strStack = new Stack<>();
strStack.push("Element 1");
strStack.push("Element 2");
strStack.push("Element 3");
strStack.pop();
strStack.pop();
System.out.println(strStack);
}
public static void main(String args[]) {
StackExample test = new StackExample();
test.createStack();
}
}
code block 1-3: Code snippets that delete values from Stack data structure and print.
As Stack follows LIFO order, The code above deletes the elements with the priority given to the most recently added element. Therefore, the code above outputs below:
[Element 1]
Conclusion
The most important point to remember today is the below:
Stack data structure is useful when
1. Implementation of Depth First Search (DFS) algorithm
2. Calling recursive functions
as it follows LIFO order.
'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 - 2: Queue data structure (0) | 2022.08.05 |
- Total
- Today
- Yesterday
- Spring Boot
- @RequestBody
- json web token
- google cloud
- Firebase
- 인증/인가
- DeSerialization
- spring
- FCM
- docker
- 가상 서버
- DTO
- 코테
- Java Data Types
- 실시간데이터
- ci/cd
- 역직렬화
- JPA
- JPQL
- JOIN FETCH
- 기지국 설치
- 알고리즘
- 깃랩
- LazyInitializationException
- 지연 로딩
- N+1
- 도커
- gitlab
- 프로그래머스
- Jackson
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |