//Stack
- import java.util.Stack;
- Stack의 선언 및 생성
Stack st = new Stack();
import java.util.Stack;
public class Ex03 {
public static void main(String[] args) {
Stack st = new Stack();
st.push("0");
st.push("1");
st.push("2");
System.out.println("= Stack =");
while(!st.empty()) { //비어 있지 않을 때까지 반복
System.out.println("맨윗 값 : " + st.peek());
System.out.println("꺼내는 값 : " + st.pop());
}
}
}
- 값 추가 : Stack에 차곡차곡 객체를 저장.
st.push(item);
- Stack이 비어있는지 확인 : 비어 있으면 True / 비어 있지 않으면 False 반환
st.empty();
- Stack의 맨 위 객체 반환
st.peek();
- 값 삭제 : 맨 위에 저장된 객체를 꺼냄
st.pop();
import java.util.EmptyStackException;
import java.util.Stack;
public class Ex05 {
public static void main(String[] args) {
Stack st = new Stack();
String expression = "((3+5*8-2))))"; // args[0]
System.out.println("expression: " + expression);
try {
for (int i = 0; i < expression.length(); i++) {
char ch = expression.charAt(i);
if (ch == '(') {
st.push(ch + "");
} else if (ch == ')') {
st.pop();
}
}
if (st.empty()) {
System.out.println("괄호가 일치합니다.");
} else {
System.out.println("괄호가 일치하지 않습니다.");
}
} catch (EmptyStackException e) {
System.out.println("괄호가 일치하지 않습니다.");
}
}
}
import java.util.EmptyStackException;
import java.util.Stack;
public class Ex05 {
public static void main(String[] args) {
Stack st = new Stack();
String expression = "((3+5*8-2))"; // args[0]
System.out.println("expression: " + expression);
try {
for (int i = 0; i < expression.length(); i++) {
char ch = expression.charAt(i);
if (ch == '(') {
st.push(ch + "");
} else if (ch == ')') {
st.pop();
}
}
if (st.empty()) {
System.out.println("괄호가 일치합니다.");
} else {
System.out.println("괄호가 일치하지 않습니다.");
}
} catch (EmptyStackException e) {
System.out.println("괄호가 일치하지 않습니다.");
}
}
}
//Queue
- import java.util.LinkedList;
- import java.util.Queue;
- Queue의 선언 및 생성
Queue q = new LinkedList();
import java.util.LinkedList;
import java.util.Queue;
public class Ex04 {
public static void main(String[] args) {
Queue q = new LinkedList();
q.offer("0");
q.offer("1");
q.offer("2");
System.out.println("= Queue =");
while(!q.isEmpty()) { //비어 있지 않을 동안 반복
System.out.println("가장 앞의 값 : " + q.peek());
System.out.println("꺼내는 값 : " + q.poll());
}
}
}
- 값 추가 : Queue에 차곡차곡 객체를 저장.
q.offer(o);
- Queue가 비어있는지 확인 : 비어 있으면 True / 비어 있지 않으면 False 반환
q.isEmpty();
- Queue의 맨 앞 객체 반환
q.peek();
- 값 삭제 : 맨 앞에 저장된 객체를 꺼냄
q.poll();
import java.util.Queue;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
public class Ex06 {
static Queue q = new LinkedList();
static final int MAX_SIZE = 5;
public static void main(String[] args) {
System.out.println("help를 이용하면 도움말을 볼 수 있습니다.");
while(true) {
System.out.println(">>");
try {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().trim();
if("".equals(input)) continue;
if(input.equalsIgnoreCase("q")) {
System.exit(0);
}else if(input.equalsIgnoreCase("help")) {
System.out.println(" help - 도움말을 보여줍니다. ");
System.out.println(" q 또는 Q - 프로그램을 종료합니다. ");
System.out.println(" history - 최근에 입력한 명령어를 " + MAX_SIZE + "개 보여줍니다. ");
} else if(input.equalsIgnoreCase("history")) {
int i = 0;
save(input);
LinkedList tmp = (LinkedList)q;
ListIterator it = tmp.listIterator();
while(it.hasNext()) {
System.out.println(++i + "." + it.next());
}
}else {
save(input);
System.out.println(input);
} //end of if(input.equalsIgnorecase("q"))
} catch(Exception e) {
System.out.println("입력 오류입니다.");
}
} //end of while(true)
} //end of main()
public static void save(String input) {
if(!"".equals(input)) {
q.offer(input);
}
if(q.size() > MAX_SIZE) {
q.remove();
}
}//end of save()
} //end of class
package collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueRunner {
public static void main(String[] args) {
Queue<String> queue = new PriorityQueue<>(new StringLengthComparator());
queue.addAll(List.of("Zebra", "Monkey", "Cat"));
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
} //main
} //QueueRunner
class StringLengthComparator implements Comparator<String> {
@Override
public int compare(String value1, String value2) {
//오름차순
return Integer.compare(value1.length(), value2.length());
// 내림차순
//return Integer.compare(value2.length(), value1.length());
}
}
'자바(JAVA)' 카테고리의 다른 글
[자바(Java)] Arrays (0) | 2022.12.16 |
---|---|
[자바(Java)] Iterator (0) | 2022.12.15 |
[자바(Java)] ArrayList (0) | 2022.12.15 |
[자바(Java)] ★객체 지향 언어★ (0) | 2022.11.15 |
[자바(Java)] Map (0) | 2022.11.02 |