1.스택(Stack)
스택과 큐는 자료구조를 공부한다면 바로 접할 수 있는 그 스택과 큐다.
스택은 LIFO(Last in First Out)으로 마지막으로 들어온 자료가 처음으로 나온다. 의미 그대로의 자료 구조를 가지고 있다.
수건을 예로 든다면 잘 개어진 수건을 사용한다고 할 때에 수건을 개어서 쌓아서 보관을 한다.
1. 쌓아서 차곡차곡 위에 올려서 보관하고
2. 꺼내 쓸 때에는 맨 위부터 하나씩 빼게 된다.
스택의 자료구조도 이와 같은 느낌이다.
Java에서도 사용 할 수 있게 구현이 되어있다 다음 예를 통해 메소드 활용과 함께 확인해보자
package chap09.exam05.stack; public class Towel { private String color; public Towel(String color) { this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
package chap09.exam05.stack; import java.util.Stack; public class TowelBox { public static void main(String[] args) { // 수건통 Stack<Towel> box = new Stack<Towel>(); // 넣기(push) box.push(new Towel("red")); box.push(new Towel("orange")); box.push(new Towel("yellow")); box.push(new Towel("green")); box.push(new Towel("blue")); box.push(new Towel("blush violet")); box.push(new Towel("purple")); System.out.println(box.size()); // 수건 빼기 Towel towel = box.pop(); System.out.println(towel.getColor()); System.out.println(box.size()); // 타올을 뺀 다음 색상 확인(메소드 체이닝) System.out.println(box.pop().getColor()); System.out.println(box.size()); // 남아있는 수건을 하나씩 뽑아서 모두 색상을 확인 int number = box.size(); for(int i = 0; i < number; i++) { System.out.println(box.pop().getColor()); System.out.println(box.size()); } // for문이나 while문 셋 중 두개만 주석(ctrl + /)처리해서 // 테스트 해보면 같은 결과를 얻을 수 있음 while(box.size() > 0) { // pop은 값을 확인하고 버림 System.out.println(box.pop().getColor()); // peek은 값을 확인만 함 // System.out.println(box.peek().getColor()); System.out.println(box.size()); } // 향상된 for에서 내부적으로 pop을 사용하지 않고 있음 for(Towel item:box) { System.out.println(item.getColor()); System.out.println(box.size()); } } }
2.큐(Queue)
큐는 스택과 마찬가지로 대표적인 자료구조로 FIFO구조로 이루어진다.
First In First Out이고 처음 들어온 자료가 처음부터 나가는것이다.
마찬가지로 예를 든다면
1. 가게에 물건을 사러 1~5번 손님이 번호에 맞춰 순서대로 계산대에 서게 된다면
2. 1번 손님이 먼저 왔으니 먼저 계산하는 구조
이와 같이 큐는 들어온 순서대로 나가는 구조이다.
package chap09.exam06.queue; public class Job { private String command; private String to; public Job(String command, String to) { this.command = command; this.to = to; } //command와 to의 수정을 허용하지 않으므로 getter만 만듬 public String getCommand() { return command; } public String getTo() { return to; } }
package chap09.exam06.queue; import java.util.LinkedList; import java.util.Queue; public class JobList { public static void main(String[] args) { // Queue 생성 - 내부구조는 링크드 리스트 Queue<Job> list = new LinkedList<Job>(); // 값을 넣을 때 offer list.offer(new Job("Send SMS", "A")); list.offer(new Job("Send Mail", "B")); list.offer(new Job("Send SMS", "C")); list.offer(new Job("Send Kakao", "D")); list.offer(new Job("Send Call", "E")); // 등록된 해야할 일(누구에게 무얼 하는지)을 모두 확인 하기 // 값을 뺄 때 poll // peek 확인만 할 때 // size를 통해 크기 확인 // isEmpty // System.out.println(list.isEmpty()); // System.out.println(list.size()); // System.out.println(list.poll().getTo() + "의 다음 사람에게 할 일 : " + list.poll().getCommand()); // System.out.println(list.poll().getTo() + "의 다음 사람에게 할 일 : " + list.poll().getCommand()); // System.out.println(list.peek().getTo() + "에게 할 일 : " + list.poll().getCommand()); // System.out.println(list.size()); // System.out.println(list.isEmpty()); while(list.size() > 0) { System.out.println("남은 작업 개수 : " + list.size()); Job j = list.poll(); System.out.println(j.getCommand() + " to " + j.getTo()); } } }
주석문은 직접 지웠다가 다시 붙였다가 하며 테스트를 해보자(Ctrl+/)
'개념 및 코딩 > 07.Collection Framework' 카테고리의 다른 글
[JAVA]07-04.HashMap, HashTable (0) | 2018.09.04 |
---|---|
[JAVA]07-03.Hashset (0) | 2018.09.04 |
[JAVA]07-02.ArrayList, LinkedList (0) | 2018.09.04 |
[JAVA]07-01.Collection, Map (0) | 2018.09.04 |