ArrayList를 stack과 queue 구현하기
- Stack(Last In First Out(FIFO): 맨 마지막에 추가 된 요소가 가장 먼저 꺼내지는 자료구조
- 이미구현된 클래스가 제공됨
- ArrayList, LinkedList로 구현할 수 있음
- 게임에서 무르기, 최근자료 가져오기 등의 구현에 사용
Queue 구현하기
- First In First Out(FIFO) : 먼저 저장된 자료가 먼저 꺼내지는 자료구조
- 선착순, 대기열등을 구현할 때 가장 많이 사용되는 자료 구조
- ArrayList나 LinkedList 로 구현 할 수 있음
package collection;
import java.util.ArrayList;
class MyStack {
private ArrayList<String> arrayStack = new ArrayList<String>();
public void push(String data) {
arrayStack.add(data);
}
public String pop() {
int len = arrayStack.size();
if(len==0) {
System.out.println("스택이 비었습니다.");
return null;
}
return arrayStack.remove(len-1);
}
//javaDoc에서 Stack을 검색하면 내장 되어 있는 도큐먼트를 확인 가능하다.
}
public class StackTest{
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
반응형
'JAVA' 카테고리의 다른 글
Comparable 인터페이스와 Comparator 인터페이스 (0) | 2019.10.11 |
---|---|
set 인터페이스 (0) | 2019.10.10 |
List 인터페이스 (0) | 2019.10.08 |
제네릭 (0) | 2019.10.07 |
컬렉션 프레임워크(Collection Framework) (0) | 2019.10.06 |