Skip to content

Latest commit

ย 

History

History
144 lines (105 loc) ยท 3.38 KB

File metadata and controls

144 lines (105 loc) ยท 3.38 KB

03. ์Šคํƒ๊ณผ ํ(p.144-148)

์Šคํƒ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ์Šคํƒ

    • ๋ฐ์ดํ„ฐ๋ฅผ ์Œ“์•„ ์˜ฌ๋ฆฐ๋‹ค(stack).
    • LIFO(Last-In-First-Out)์— ๋”ฐ๋ผ ์ž๋ฃŒ๋ฅผ ๋ฐฐ์—ดํ•œ๋‹ค.
    • ๋ฐฐ์—ด๊ณผ ๋‹ฌ๋ฆฌ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— i๋ฒˆ์งธ ํ•ญ๋ชฉ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค. ํ•˜์ง€๋งŒ ์Šคํƒ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๋Š” ์—ฐ์‚ฐ์€ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์—ฐ์‚ฐ

    • pop()
    • push(item)
    • peek()
    • isEmpty()
  • ๊ตฌํ˜„

    • ์Šคํƒ์€ ๊ฐ™์€ ๋ฐฉํ–ฅ์—์„œ ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ญ์ œํ•œ๋‹ค๋Š” ์กฐ๊ฑดํ•˜์— ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    public class MyStack {
      
      private static class StackNode {
        private T data;
        private StackNode next;
        
        public StackNode(T data) {
          this.data = data;
        }
      }
      
      private StackNode top;
      
      public T pop() {
        if (top == null) throw new EmptyStackException();
        T item = top.data;
        top = top.next;
        return item;
      }
      
      public void push(T item) {
        StackNode t = new StackNode(item);
        t.next = top;
        top = t;
      }
      
      public T peek() {
         if (top == null) throw new EmptyStackException();
    		return top.data;
      }
      
      public boolean isEmpty() {
        return top == null;
      }
    }
  • ์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

    • ์žฌ๊ท€์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ์ž„์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์Šคํƒ์— ๋„ฃ์–ด ์ฃผ๊ณ , ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ๋น ์ ธ ๋‚˜์™€ **ํ‡ด๊ฐ ๊ฒ€์ƒ‰(backtrack)**์„ ํ• ๋•Œ๋Š” ์Šคํƒ์— ๋„ฃ์–ด ๋‘์—ˆ๋˜ ์ž„์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋นผ ์ค˜์•ผ ํ•œ๋‹ค.
  • ์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐ˜๋ณต์  ํ˜•ํƒœ(iterative)๋ฅผ ํ†ตํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

ํ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ํ

    • FIFO(First-In-First-Out)
  • ์—ฐ์‚ฐ

    • add(item)
    • remove()
    • peek()
    • isEmpty()
  • ๊ตฌํ˜„

    • ํ ๋˜ํ•œ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์—์„œ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด ๊ทผ๋ณธ์ ์œผ๋กœ ํ์™€ ๊ฐ™๋‹ค.
    public class MyQueue {
      
      private static class QueueNode {
        private T data;
        private QueueNode next;
        
        public QueueNode(T data) {
          this.data = data;
        }
      }
      
      private QueueNode first;
      private QueueNode last;
      
      public void add(T item) {
        QueueNode t = new QueueNode(item);
        if (last != null) {
          last.next = t;
        }
        last = t;
        if (first == null) {
          first = last;
        }
      }
      
      public T remove() {
        if (first == null) throw new NoSuchElementException();
        T data = first.data;
    		first = first.next;
        if (first == null) {
          last = null;
        }
        return data;
      }
      
      public T peek() {
        if (first == null) throw new NoSuchElementException();
        return first.data;
      }
      
      public boolean isEmpty() {
        return first == null;
      }
    }
  • ํ์—์„œ ์ฒ˜์Œ๊ณผ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๊ฐฑ์‹ ํ•  ๋•Œ ์‹ค์ˆ˜๊ฐ€ ๋‚˜์˜ค๊ธฐ ์‰ฝ๋‹ค.

  • ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(breadth-first search)์„ ํ•˜๊ฑฐ๋‚˜ ์บ์‹œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ์— ์ข…์ข… ์‚ฌ์šฉ๋œ๋‹ค.

    • ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ์ฒ˜๋ฆฌํ•  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ ํ์— ๋‹ค์‹œ ์ €์žฅํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋…ธ๋“œ๋ฅผ ์ ‘๊ทผํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋ฉด์ ‘ ๋ฌธ์ œ

  1. ํ•œ ๊ฐœ๋กœ ์„ธ ๊ฐœ
  2. ์Šคํƒ Min
  3. ์ ‘์‹œ ๋ฌด๋”๊ธฐ
  4. ์Šคํƒ์œผ๋กœ ํ
  5. ์Šคํƒ ์ •๋ ฌ
  6. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ