Backend
home
🥡

Stack/Queue

Stack Example
package com.collection.stack; import lombok.AllArgsConstructor; import lombok.Getter; @AllArgsConstructor @Getter public class Coin { private int value; }
Java
복사
package com.collection.stack; import java.util.Stack; public class StackExample { public static void main(String[] args) { Stack<Coin> coinBox = new Stack<>(); coinBox.push(new Coin(500)); coinBox.push(new Coin(100)); coinBox.push(new Coin(50)); coinBox.push(new Coin(10)); coinBox.push(new Coin(5)); coinBox.push(new Coin(1)); while (!coinBox.isEmpty()) { Coin c = coinBox.pop(); System.out.println("꺼낸 동전 : " + c.getValue() + "원, " + coinBox.size() + "개 남음"); } } }
Java
복사
Queue Example
package com.collection.queue; import lombok.AllArgsConstructor; import lombok.Getter; @AllArgsConstructor @Getter public class Message { private String command, to; }
Java
복사
package com.collection.queue; import lombok.AllArgsConstructor; import lombok.Getter; import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queue<Message> msgQueue = new LinkedList<>(); msgQueue.offer(new Message("sendMail", "허클베리피")); msgQueue.offer(new Message("Call", "팔로알토")); msgQueue.offer(new Message("sendKakao", "E-sens")); msgQueue.offer(new Message("CallDeny", "Uneducated Kids")); while (!msgQueue.isEmpty()) { Message msg = msgQueue.poll(); switch (msg.getCommand()) { case "sendMail": System.out.println(msg.getTo() + "에게 메일을 보냅니다."); break; case "Call": System.out.println(msg.getTo() + "에게 전화합니다."); break; case "sendKakao": System.out.println(msg.getTo() + "에게 카톡합니다."); break; case "CallDeny": System.out.println(msg.getTo() + "에게 오는 전화는 무시합니다."); break; } } } }
Java
복사