ποΈ Backend/Java
μλ°(Java) - 컬λ μ νλ μμν¬(6) (Stack)LIFO, (Queue)FIFO
kongmi
2023. 2. 2. 19:45
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(); // νμ κ°μκ° λμ΄κ°λ©΄ 맨 μμ μμ μ κ±°
}
}