GiantStepDEV
article thumbnail

Stack?

  • LIFO(Last in First Out) ๊ตฌ์กฐ, ์Šคํƒ์„ ์ด์šฉํ•œ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๋กœ JVM์˜ ์Šคํƒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์Šคํƒ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋œ ๋ณ€์ˆ˜๋Š” ๋‚˜์ค‘์— ์ €์žฅ๋œ ๊ฒƒ ๋ถ€ํ„ฐ ๋จผ์ € ์ œ๊ฑฐ ๋ฉ๋‹ˆ๋‹ค.
  • ์Šคํƒ์€ ๋‚ด๋ถ€์ ์œผ๋กœ Vector ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„ ๋งŒ๋“ค์–ด ์กŒ์Šต๋‹ˆ๋‹ค.
  •  ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ๋ฉ”์†Œ๋“œ์˜ ํ˜ธ์ถœ์ด ์Œ“์ด๋Š” ๊ตฌ์กฐ์— ์‚ฌ์šฉ ๋ฉ๋‹ˆ๋‹ค.
  • ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด ํ•œ ๊ณณ์—์„œ๋งŒ ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค.

๊ด€๋ จ ๋ฉ”์†Œ๋“œ

  1. push() : ๊ฐ์ฒด๋ฅผ ์ €์žฅ ํ•ฉ๋‹ˆ๋‹ค.
    public E push(E item)
  2. peek() : ์Šคํƒ ๋งจ ์œ„์˜ ๊ฐ’ ํ™•์ธ
  3. pop() : ์Šคํƒ์˜ ๋งจ ์œ„์˜ ๊ฐ’์„ ์ถ”์ถœํ•˜๋ฉด์„œ ๋ณด์—ฌ์คŒ
    public E pop() 
    โžก๏ธ Removes the object at the top of this stack and returns that object as the value of this function.
  4. empty() : ์Šคํƒ์ด ๋น„์–ด ์žˆ๋Š”์ง€ ํ™•์ธ ํ›„ boolean์œผ๋กœ ๋ฐ˜ํ™˜
  5. size() : ์Šคํƒ์— ํฌํ•จ๋œ ๊ฐ์ฒด ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜(int)
  6. 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.

  1. 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.
  2. 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

  1. remove() - ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ
  2. 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

  1. element() - ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ์ฝ์–ด์˜ด - ์˜ˆ์™ธ ๋ฐœ์ƒ
    โœ… This method differs from peek only in that it throws an exception if this queue is empty.
  2. 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(); // ํ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ๋งจ ์•ž์˜ ์š”์†Œ ์ œ๊ฑฐ
    }
}

 

profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!