반응형

Stack

한 쪽 끝에서만 자료를 넣고 뺄 수 있는 LIFO(Last In First Out) 형식의 자료구조

package chap12.sec02.exam01;

import java.util.Stack;

/**
 * packageName : chap12.sec02.exam01
 * fileName : StackApplication
 * author : GGG
 * date : 2023-09-26
 * description : 스택(Stack)에 대한 소개 예제
 * 요약 :
 *         Stack(스택) : LIFO (Last In First Out)  -> 코딩 : Stack 클래스
 *         Queue(큐 : 인터페이스)  : FIFO (First in First out)  -> 코딩 : LinkedList 사용
 *
 * <p>
 * ===========================================================
 * DATE            AUTHOR             NOTE
 * —————————————————————————————
 * 2023-09-26         GGG          최초 생성
 */
public class StackApplication {
    public static void main(String[] args) {

//        TODO : 스택 객체 정의
        Stack<Coin> coinStack = new Stack<>();
//        TODO : Coin 객체 추가 : .push(값)
        coinStack.push(new Coin(100));          // 100원  (1st)
        coinStack.push(new Coin(50));           // 50원
        coinStack.push(new Coin(500));          // 500원
        coinStack.push(new Coin(10));           // 10원   (last)     => 꺼낼때 먼저 나옴


//        TODO : Coin 객체 꺼내기 : .pop() (순차적으로 마지막 값을 꺼냄)
//         참고) 현재 마지막 값 조회하기 : .peek()
        while (coinStack.isEmpty() == false){
            Coin coin = coinStack.pop();        // 마지막 코인 꺼내기
            System.out.println("꺼낸 동전 : " + coin.getValue());
        }

        
    }
}

 

 

 

 

Queue

데이터를 일시적으로 쌓아두기 위한 형식, FIFO(First In First Out) 형식의 자료구조

package chap12.sec02.exam02;

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

/**
 * packageName : chap12.sec02.exam02
 * fileName : MessageApplication
 * author : GGG
 * date : 2023-09-26
 * description :    큐 소개 예제 : FIFO
 * 요약 :
 * <p>
 * ===========================================================
 * DATE            AUTHOR             NOTE
 * —————————————————————————————
 * 2023-09-26         GGG          최초 생성
 */
public class MessageApplication {
    public static void main(String[] args) {
//        TODO: 큐(인터페이스) 객체 생성 - LinkedList 사용
        Queue<Message> messageQueue = new LinkedList<>();

//        TODO: 큐에 자료저장 : .offer(값) 또는 .add(값)
        messageQueue.offer(new Message("sendMail", "홍길동"));
        messageQueue.offer(new Message("sendSMS", "신용권"));
        messageQueue.offer(new Message("sendKakao", "홍두깨"));

//        TODO: 꺼내기 : .poll()
        while (messageQueue.isEmpty() == false){
            Message message = messageQueue.poll();      // 1st 꺼내기
            System.out.println(message.to);             // 보낼 사람 이름
            
        }


    }
}

반응형

'Java > Java 이론' 카테고리의 다른 글

JAVA API 함수  (0) 2023.09.26
예외처리  (0) 2023.09.26
객체의 동등비교  (1) 2023.09.25
자료구조  (0) 2023.09.25
MVC 디자인 패턴  (0) 2023.09.22

+ Recent posts