Recent Posts
Recent Comments
Link
06-30 12:53
Today
Total
관리 메뉴

삶 가운데 남긴 기록 AACII.TISTORY.COM

Stack과 Queue 본문

DEV&OPS/Java

Stack과 Queue

ALEPH.GEM 2022. 5. 10. 10:15

stack

스택은 Last In First Out 자료구조입니다.

제일 마지막에 push()한 객체가 제일 처음에 pop() 됩니다.

import java.util.Stack;

class Coin{
	private int value;
	public Coin(int value) {
		this.value = value;
	}
	public int getValue() {
		return value;
	}
}

public class StackEx {
	public static void main(String[] args) {
		Stack<Coin> coinBox = new Stack<Coin>();
		coinBox.push(new Coin(100));
		coinBox.push(new Coin(500));
		coinBox.push(new Coin(10));
		coinBox.push(new Coin(50));
		
		while(!coinBox.isEmpty()) {
			Coin coin = coinBox.pop();
			System.out.println(coin.getValue());
		}
	}
}

 

Queue

큐는 First In First Out 자료구조입니다.

제일 먼저 offer() 한 객체를 가장 먼저 poll() 합니다.

import java.util.LinkedList;
import java.util.Queue;

class Message{
	public String command;
	public String to;
	public Message(String command, String to) {
		this.command = command;
		this.to = to;
	}
}

public class QueueEx {
	public static void main(String[] args) {
		Queue<Message> messageQueue = new LinkedList<Message>();
		messageQueue.offer(new Message("sendMail", "홍길동"));
		messageQueue.offer(new Message("sendSMS", "고길동"));
		messageQueue.offer(new Message("sendMessage", "김길동"));
		
		while(!messageQueue.isEmpty()) {
			Message message = messageQueue.poll();
			switch(message.command) {
				case "sendMail":
					System.out.println(message.to+"에게 메일 전송");
					break;
				case "sendSMS":
					System.out.println(message.to+"에게 SMS 전송");
					break;
				case "sendMessage":
					System.out.println(message.to+"에게 메시지 전송");
					break;
			}
		}
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90

'DEV&OPS > Java' 카테고리의 다른 글

자바 콘솔 입출력  (0) 2022.05.17
자바 stream  (0) 2022.05.17
컬랙션 검색, 병렬처리, 동기화  (0) 2022.05.09
Map 컬렉션  (0) 2022.05.09
Set 컬렉션  (0) 2022.05.09