Stack?
- LIFO(Last in First Out) ๊ตฌ์กฐ, ์คํ์ ์ด์ฉํ ๋ํ์ ์ธ ์์๋ก JVM์ ์คํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ค ์ ์์ต๋๋ค.
- ์คํ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋ ๋ณ์๋ ๋์ค์ ์ ์ฅ๋ ๊ฒ ๋ถํฐ ๋จผ์ ์ ๊ฑฐ ๋ฉ๋๋ค.
- ์คํ์ ๋ด๋ถ์ ์ผ๋ก Vector ํด๋์ค๋ฅผ ์์ ๋ฐ์ ๋ง๋ค์ด ์ก์ต๋๋ค.
- ํ๋ก๊ทธ๋จ ๋ด์์ ๋ฉ์๋์ ํธ์ถ์ด ์์ด๋ ๊ตฌ์กฐ์ ์ฌ์ฉ ๋ฉ๋๋ค.
- ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ด ํ ๊ณณ์์๋ง ์ผ์ด๋ฉ๋๋ค.
๊ด๋ จ ๋ฉ์๋
- push() : ๊ฐ์ฒด๋ฅผ ์ ์ฅ ํฉ๋๋ค.
public E push(E item)
- peek() : ์คํ ๋งจ ์์ ๊ฐ ํ์ธ
- pop() : ์คํ์ ๋งจ ์์ ๊ฐ์ ์ถ์ถํ๋ฉด์ ๋ณด์ฌ์ค
public E pop()
โก๏ธ Removes the object at the top of this stack and returns that object as the value of this function. - empty() : ์คํ์ด ๋น์ด ์๋์ง ํ์ธ ํ boolean์ผ๋ก ๋ฐํ
- size() : ์คํ์ ํฌํจ๋ ๊ฐ์ฒด ๊ฐ์ ๋ฐํ(int)
- contains(Object o) : ํด๋น ๊ฐ(Object o)์ด ์คํ ์์ ์๋์ง boolean์ผ๋ก ๋ฐํ
public boolean contains(Object o)
๐ถ๊ธฐ๋ณธ ์์ 1
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);
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.empty());
System.out.println(stack.size());
System.out.println(stack.contains(1));
}
}
3
3
false
2
true
๐ถ์์ฉ ์์ (๋์ )
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Coin> coinBox = new Stack<>();
coinBox.push(new Coin(100));
coinBox.push(new Coin(50));
coinBox.push(new Coin(500));
coinBox.push(new Coin(10));
while(!coinBox.isEmpty()) {
Coin coin = coinBox.pop();
System.out.println("๊บผ๋ด ์จ ๋์ : " + coin.getValue());
}
}
}
class Coin {
private int value;
public Coin(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
๊บผ๋ด ์จ ๋์ : 10
๊บผ๋ด ์จ ๋์ : 500
๊บผ๋ด ์จ ๋์ : 50
๊บผ๋ด ์จ ๋์ : 100
๐ถ์์ฉ ์์ 2 (๊ดํธ)
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Character> st = new Stack<>();
Scanner sc = new Scanner(System.in);
System.out.print("์์ ์
๋ ฅ : ");
String exp = sc.next();
for(int i = 0; i < exp.length(); i++) {
char ch = exp.charAt(i);
if (ch == '(') { // ์ด๋ฆผ ๊ดํธ์ผ ๋ push
st.push(ch);
} else if (ch == ')') { // ๋ซํ ๊ดํธ์ผ ๋
if (!st.isEmpty()) st.pop(); // stack์ด ๋น์ด์์ง ์์ผ๋ฉด pop
else {
System.out.println("๊ดํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค.");
return;
}
}
}
if(st.isEmpty()) {
System.out.println("๊ดํธ๊ฐ ์ผ์น ํฉ๋๋ค.");
} else {
System.out.println("๊ดํธ๊ฐ ์ผ์น ํ์ง ์์ต๋๋ค.");
}
}
}
์์ ์
๋ ฅ : ((3+4)*6)
๊ดํธ๊ฐ ์ผ์น ํฉ๋๋ค.
์์ ์
๋ ฅ : ((3+4) *6))
๊ดํธ๊ฐ ์ผ์น ํ์ง ์์ต๋๋ค.
Queue?
- FIFO(First In First Out) ๊ตฌ์กฐ
- LinkedList ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ, LinkedList ๊ธฐ๋ฅ ์ค Queue ๊ด๋ จ ๊ธฐ๋ฅ์ผ๋ก ์ ํ๋จ
๊ด๋ จ ๋ฉ์๋
Summary of Queue methods
Throws exception | Returns special value | |
Insert | add(e) | offer(e) |
Remove | remove() | poll() |
Examine | element() | peek() |
(1) Parameters: e
the element to add
๐ก In a FIFO queue, all new elements are inserted at the tail of the queue.
- add(e) : ๋งจ ๋ค ์์น์ ์์๋ฅผ ์ฝ์
(์ค๊ฐ ์ฝ์
X) - ์์ธ ๋ฐ์
โ Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. - offer(e) : ๋งจ ๋ค ์์น์ ์์๋ฅผ ์ฝ์
โ The offer method inserts an element if possible, otherwise returning false.
โ The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.
(2) Retrieves and removes the head of this queue.
return the head of this queue
- remove() - ๋น์ด์์ผ๋ฉด ์์ธ ๋ฐ์
- poll() - ๋น์ด์์ผ๋ฉด null ๋ฐํ
๐กThe remove() and poll() methods differ only in their behavior when the queue is empty: the remove() method throws an exception, while the poll() method returns null.
(3) Retrieves, but does not remove, the head of this queue.
return the head of this queue
- element() - ๋งจ ์์ ์์๋ฅผ ์ฝ์ด์ด - ์์ธ ๋ฐ์
โ This method differs from peek only in that it throws an exception if this queue is empty. - peek() - ๋งจ ์์ ์์๋ฅผ ์ฝ์ด์ด - ๋น์ด ์์ผ๋ฉด null ๋ฐํ
โ the head of this queue, or null if this queue is empty
๐ถ๊ธฐ๋ณธ ์์ 1
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Message> msgQ = new LinkedList<>();
msgQ.offer(new Message("Mail", "ํ๊ธธ๋"));
msgQ.add(new Message("SMS", "ํ์๋ด"));
msgQ.offer(new Message("Kakao", "์ค์ฌ๋ฏธ"));
while(!msgQ.isEmpty()) {
Message msg = msgQ.poll(); // ํ์์ ํ๊ฐ์ ๋ฉ์์ง๋ฅผ ๊บผ๋(๋งจ์)
switch(msg.command) {
case "Mail" : System.out.println(msg.to + "์๊ฒ ๋ฉ์ผ์ ๋ณด๋
๋๋ค."); break;
case "SMS" : System.out.println(msg.to + "์๊ฒ ๋ฌธ์๋ฅผ ๋ณด๋
๋๋ค."); break;
case "Kakao" : System.out.println(msg.to + "์๊ฒ ์นด์นด์คํก์ ๋ณด๋
๋๋ค."); break;
}
}
}
}
class Message {
String command;
String to;
public Message(String command, String to) {
this.command = command;
this.to = to;
}
}
ํ๊ธธ๋์๊ฒ ๋ฉ์ผ์ ๋ณด๋
๋๋ค.
ํ์๋ด์๊ฒ ๋ฌธ์๋ฅผ ๋ณด๋
๋๋ค.
์ค์ฌ๋ฏธ์๊ฒ ์นด์นด์คํก์ ๋ณด๋
๋๋ค.
๐ถ์์ฉ ์์ (๋ช ๋ น ์ด๋ ฅ ์กฐํ)
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
Queue<String> queue = new LinkedList<>();
final static int MAX_SIZE = 10;
public static void main(String[] args) {
Main history = new Main();
System.out.println("help๋ฅผ ์
๋ ฅํ๋ฉด ๋์๋ง์ ๋ณผ ์ ์์ต๋๋ค.");
while(true) {
System.out.print("$ ");
Scanner sc = new Scanner(System.in);
String cmd = sc.nextLine().trim(); // ์
๋ ฅ ๋ฐ์ ๋ฌธ์์ด์ ์๋ค์ ๊ณต๋ฐฑ ์ ๊ฑฐ
if(cmd.equalsIgnoreCase("q")) {
System.exit(0); // ํ๋ก๊ทธ๋จ ๊ฐ์ ์ข
๋ฃ (return ๋ณด๋ค ๊ฐ๋ ฅ)
} else if(cmd.equalsIgnoreCase("help")) {
System.out.println("help - ๋์๋ง์ ๋ณด์ฌ์ค๋๋ค.");
System.out.println("q/Q - ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
System.out.println("history - ์ต๊ทผ ์
๋ ฅํ ๋ช
๋ น์ด๋ฅผ " + MAX_SIZE + "๊ฐ ๋ณด์ฌ์ค๋๋ค.");
} else if(cmd.equalsIgnoreCase("history")) {
history.save(cmd);
int cnt = 0;
for(String e : history.queue) {
cnt++;
System.out.println(cnt + " " + e);
}
} else {
history.save(cmd);
System.out.println(cmd);
}
}
}
void save(String cmd) {
queue.offer(cmd);
if(queue.size() > MAX_SIZE) queue.remove(); // ํ์ ๊ฐ์๊ฐ ๋์ด๊ฐ๋ฉด ๋งจ ์์ ์์ ์ ๊ฑฐ
}
}