스택
후입선출
(LIFO : Last In, First Out)
수정자 및 타입 | 메서드 설명 |
bollean | public boolean empty() 이 스택이 비어 있는지 테스트 Returns : 스택에 요소가 없으면 true 스택에 요소가 있으면 false |
E | public E peek() 스택에서 object를 제거하지 않고 이 스택의 맨 위에 있는 객체를 확인 Returns : 이 스택의 맨 위에 있는 object Throws : EmptyStackExcption : 이 스택이 비어 있는 경우 |
E | public E pop() 이 스택의 맨 위에 있는 object를 제거하고 해당 object를 이 함수의 값으로 반환 Returns : 이 스택의 맨 위에 있는 object Throws : EmptyStackExcption : 이 스택이 비어 있는 경우 |
E | public E push(E item) 이 스택의 맨 위로 항목을 넣기 Parameters : 이 스택에 넣을 요소 Returns : 그 요소의 인수 |
int | public int search(Object o) 이 스택의 object가 있는 1을 시작으로 위치를 반환한다. 스택의 맨 위에서 가장 가까운 위치에 나오는 object의 거리를 반환한다. 스택의 맨위는 1. Parameters : 원하는 object Returns : object가 있는 스택의 맨 위를 시작지점으로 1부터 증가하는 거리 |
실제로 해보는 것이 더 이해하기 쉬울 것이다.
예제
push(E item)
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1); // 스택에 1 추가
stack.push(2); // 스택에 2 추가
stack.push(3); // 스택에 3 추가
stack.push(4); // 스택에 4 추가
stack.push(6); // 스택에 6 추가
System.out.println("stack = " + stack); // 스택 출력
}
}
-----------------------------------------------------------------
stack = [1, 2, 3, 4, 6]
Process finished with exit code 0
pop()
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(6);
System.out.println("stack = " + stack);
int popped = stack.pop();
System.out.println("Popped element: " + popped);
System.out.println("Stack after popping: " + stack);
}
}
---------------------------------------------------------------------
stack = [1, 2, 3, 4, 6]
Popped element: 6
Stack after popping: [1, 2, 3, 4]
peek()
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(6);
int topElement = stack.peek();
System.out.println("Top element: " + topElement);
System.out.println("Stack after peek: " + stack);
}
}
-------------------------------------------------------------------
Top element: 6
Stack after peek: [1, 2, 3, 4, 6]
Process finished with exit code 0
search(Object o)
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
// 스택에 요소 추가
stack.push("Java");
stack.push("Python");
stack.push("C++");
System.out.println("stack = " + stack);
// "Python" 요소의 인덱스 검색
int index = stack.search("Python");
System.out.println("찾을 요소 : Python");
if (index != -1) {
System.out.println("찾는 요소의 인덱스: " + index);
} else {
System.out.println("해당 요소를 찾을 수 없습니다.");
}
}
}
-----------------------------------------------------------------------
stack = [Java, Python, C++]
찾을 요소 : Python
찾는 요소의 인덱스: 2
Process finished with exit code 0
empty()
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
System.out.println("stack = " + stack);
// 스택이 비어있는지 확인
if (stack.empty()) {
System.out.println("스택이 비어있습니다.");
}
// 스택에 요소 추가
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("stack = " + stack);
// 스택이 비어있는지 다시 확인
if (!stack.empty()) {
System.out.println("스택이 비어있지 않습니다.");
}
}
}
---------------------------------------------------------------------------
stack = []
스택이 비어있습니다.
stack = [1, 2, 3]
스택이 비어있지 않습니다.
Process finished with exit code 0
'자바 탐구' 카테고리의 다른 글
Intellij) 테마 바꾸기 (0) | 2023.04.03 |
---|---|
인터페이스) Comparator (0) | 2023.03.21 |
인터페이스) Comparable (0) | 2023.03.18 |
자료구조) 큐 (0) | 2023.03.13 |
자료구조) 우선순위 큐 (0) | 2023.03.13 |