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
๋ณต์‚ฌ