Home > Algorithm > 2024 > πŸ“¦[DS,Algorithm] Deque(데크, 덱)

πŸ“¦[DS,Algorithm] Deque(데크, 덱)
DataStructure Algorithm

1️⃣ Deque(덱, Double Ended Queue)

Deque(덱, Double Ended Queue)λŠ” μ–‘μͺ½ λμ—μ„œ μ‚½μž…κ³Ό μ‚­μ œλ₯Ό ν•  수 μžˆλŠ” 자료 κ΅¬μ‘°μž…λ‹ˆλ‹€.

Javaμ—μ„œλŠ” java.util νŒ¨ν‚€μ§€μ—μ„œ μ œκ³΅ν•˜λŠ” Deque μΈν„°νŽ˜μ΄μŠ€μ™€ 이λ₯Ό κ΅¬ν˜„ν•œ 클래슀인 ArrayDeque 와 LinkedList λ₯Ό 톡해 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Deque λŠ” 큐(Queue)와 μŠ€νƒ(Stack)의 κΈ°λŠ₯을 λͺ¨λ‘ ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

1️⃣ 데크 κΈ°λ³Έ ꡬ쑰

  • 데크의 κΈ°λ³Έ κ΅¬μ‘°λŠ” μ–‘λ°©ν–₯μ—μ„œ μ‚½μž… μ‚­μ œ κ°€λŠ₯ν•œ ꡬ쑰

  • 일뢀 κΈ°λŠ₯을 μ œν•œν•˜μ—¬ μš©λ„μ— 맞게 λ³€ν˜• κ°€λŠ₯

  • addλ‚˜ remove 계열은 μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚΅λ‹ˆλ‹€.
    • λ•Œλ¬Έμ— μ˜ˆμ™Έ μ²˜λ¦¬κ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€.
  • offerμ΄λ‚˜ poll 계열은 nullμ΄λ‚˜ falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
    • λ•Œλ¬Έμ— returnκ°’ (λ°˜ν™˜κ°’)을 λ°›μ•„μ„œ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

2️⃣ Deque의 μ£Όμš” λ©”μ„œλ“œ.

1️⃣ μ‚½μž… μ—°μ‚°.

  • addFirst(E e) : μ§€μ •λœ μš”μ†Œλ₯Ό 덱의 μ•žμͺ½μ— μΆ”κ°€ν•©λ‹ˆλ‹€.
  • addLast(E e) : μ§€μ •λœ μš”μ†Œλ₯Ό 덱의 λ’€μͺ½μ— μΆ”κ°€ν•©λ‹ˆλ‹€.
  • offerFirst(E e) : μ§€μ •λœ μš”μ†Œλ₯Ό 덱의 μ•žμͺ½μ— μΆ”κ°€ν•©λ‹ˆλ‹€.
  • offerLast(E e) : μ§€μ •λœ μš”μ†Œλ₯Ό 덱의 λ’€μͺ½μ— μΆ”κ°€ν•©λ‹ˆλ‹€.

2️⃣ μ‚­μ œ μ—°μ‚°.

  • removeFirst() : 덱의 μ•žμͺ½μ—μ„œ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • removeLast() : 덱의 λ’€μͺ½μ—μ„œ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • pollFirst() : 덱의 μ•žμͺ½μ—μ„œ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • pollLast() : 덱의 λ’€μͺ½μ—μ„œ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  λ°˜ν™˜ν•©λ‹ˆλ‹€.

3️⃣ 쑰회 μ—°μ‚°.

  • getFirst() : 덱의 μ•žμͺ½μ— μžˆλŠ” μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • getLast() : 덱의 λ’€μͺ½μ— μžˆλŠ” μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • peekFirst() : 덱의 μ•žμͺ½μ— μžˆλŠ” μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • peekLast() : 덱의 λ’€μͺ½μ— μžˆλŠ” μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

4️⃣ 기타 μ—°μ‚°.

  • size() : 덱에 μžˆλŠ” μš”μ†Œμ˜ 수λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • isEmpty() : 덱이 λΉ„μ–΄ μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό ν™•μΈν•©λ‹ˆλ‹€.

3️⃣ μ‹œκ°„ λ³΅μž‘λ„.

Deque μΈν„°νŽ˜μ΄μŠ€μ˜ μ‹œκ°„ λ³΅μž‘λ„λŠ” 이λ₯Ό κ΅¬ν˜„ν•œ ν΄λž˜μŠ€μ— 따라 λ‹¬λΌμ§‘λ‹ˆλ‹€.

Javaμ—μ„œλŠ” 주둜 ArrayDeque 와 LinkedList λ₯Ό μ‚¬μš©ν•˜μ—¬ Deque λ₯Ό κ΅¬ν˜„ν•©λ‹ˆλ‹€.

1️⃣ ArrayDeque

  • μ‚½μž…κ³Ό μ‚­μ œ μ—°μ‚° (μ•žκ³Ό λ’€ λͺ¨λ‘): ν‰κ· μ μœΌλ‘œ O(1)
  • 쑰회 μ—°μ‚° (μ•žκ³Ό λ’€ λͺ¨λ‘): O(1)
  • ArrayDeque λŠ” 배열을 기반으둜 κ΅¬ν˜„λ˜κΈ° λ•Œλ¬Έμ—, 배열이 꽉 μ°¨λ©΄ μžλ™μœΌλ‘œ 크기λ₯Ό λŠ˜λ¦¬μ§€λ§Œ, 이 과정은 amortized O(1)둜 κ°„μ£Όλ©λ‹ˆλ‹€.

2️⃣ LinkedList

  • μ‚½μž…κ³Ό μ‚­μ œ μ—°μ‚° (μ•žκ³Ό λ’€ λͺ¨λ‘): O(1)
  • 쑰회 μ—°μ‚° (μ•žκ³Ό λ’€ λͺ¨λ‘): O(1)
  • LinkedList λŠ” 이쀑 μ—°κ²° 리슀트둜 κ΅¬ν˜„λ˜μ–΄ μžˆμ–΄ 각 λ…Έλ“œκ°€ 이전과 λ‹€μŒ λ…Έλ“œμ— λŒ€ν•œ μ°Έμ‘°λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€.

LinkedListλŠ” 각 λ…Έλ“œκ°€ 이전 λ…Έλ“œμ™€ λ‹€μŒ λ…Έλ“œμ˜ μ°Έμ‘°λ₯Ό 가지고 μžˆμ–΄ μ‚½μž…κ³Ό μ‚­μ œκ°€ O(1)의 μ‹œκ°„ λ³΅μž‘λ„λ₯Ό κ°€μ§‘λ‹ˆλ‹€.
ν•˜μ§€λ§Œ νƒμƒ‰μ—λŠ” O(n)의 μ‹œκ°„μ΄ μ†Œμš”λ©λ‹ˆλ‹€.

ArrayDequeλŠ” 배열을 μ‚¬μš©ν•˜μ—¬ λ‚΄λΆ€μ μœΌλ‘œ κ΅¬ν˜„λ˜κΈ° λ•Œλ¬Έμ— μ‚½μž…κ³Ό μ‚­μ œ μ‹œμ—λ„ ν‰κ· μ μœΌλ‘œ O(1)의 μ‹œκ°„ λ³΅μž‘λ„λ₯Ό 가지며,
특히 큐의 λμ—μ„œμ˜ 연산이 λΉ λ¦…λ‹ˆλ‹€.

λ‹€λ§Œ, λ‚΄λΆ€μ μœΌλ‘œ 배열이 가득 μ°¨λ©΄ 크기λ₯Ό μ‘°μ •ν•΄μ•Ό ν•˜λ―€λ‘œ μ΅œμ•…μ˜ 경우 O(n)의 μ‹œκ°„ λ³΅μž‘λ„κ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Deque λŠ” λ‹€μ–‘ν•œ μƒν™©μ—μ„œ μœ μ—°ν•˜κ²Œ μ‚¬μš©λ  수 μžˆλŠ” μœ μš©ν•œ μžλ£Œκ΅¬μ‘°μž…λ‹ˆλ‹€.
특히 μ–‘μͺ½ λμ—μ„œμ˜ λΉ λ₯Έ μ‚½μž…κ³Ό μ‚­μ œκ°€ ν•„μš”ν•œ 경우 μœ μš©ν•©λ‹ˆλ‹€.

3️⃣ 직접 Deque μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„.

κ°„λ‹¨ν•œ 배열을 μ‚¬μš©ν•˜μ—¬ Deque λ₯Ό κ΅¬ν˜„ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SimpleArrayDeque<E> {
  private static final int DEFALT_CAPACITY = 10;
  private E[] elements;
  private int head;
  private int tail;
  private int size;

  public SimpleArrayDeque() {
    elements = (E[]) new Object[DEFALT_CAPACITY];
    head = 0;
    tail = 0;
    size = 0;
  }

  public void addFirst(E e) {
    if (size == elements.length) {
      resize();
    }
    head = (head - 1 + elements.length) % elements.length;
    elements[head] = e;
    size++;
  }

  public void addLast(E e) {
    if (size == elements.length) {
      resize();
    }
    elements[tail] = e;
    tail = (tail + 1) % elements.length;
    size++;
  }

  public E removeFirst() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    E element = elements[head];
    elements[head] = null; // for garbege collection
    head = (head + 1);
    size--;
    return element;
  }

  public E removeLast() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    tail = (tail - 1 + elements.length) % elements.length;
    E element = elements[tail];
    elements[tail] = null; // for garbage collection
    size--;
    return element;
  }

  public E getFirst() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    return elements[head];
  }

  public E getLast() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    return elements[(tail - 1 + elements.length) % elements.length];
  }

  public int size() {
    return size;
  }

  public boolean isEmpty() {
    return size == 0;
  }

  private void resize() {
    int newCapacity = elements.length * 2;
    E[] newElements = (E[]) new Object[newCapacity];
    for (int i = 0; i < size; i++) {
      newElements[i] = elements[(head + i) % elements.length];
    }
    elements = newElements;
    head = 0;
    tail = size;
  }

  public ArrayList<E> toArrayList() {
    return IntStream.range(0, size)
                    .mapToObj(i -> elements[(head + i) % elements.length])
                    .collect(Collectors.toCollection(ArrayList::new));
  }
}

// Main
import java.util.ArrayList;

public class Main {

  public static void main(String[] args) {
    SimpleArrayDeque<Integer> deque = new SimpleArrayDeque<>();
    deque.addFirst(1);
    deque.addLast(2);
    deque.addFirst(0);
    deque.addLast(3);

    ArrayList<Integer> dequeList = deque.toArrayList();
    System.out.println("=== dequeList === ");
    System.out.println(dequeList);


    System.out.println("First element: " + deque.getFirst());
    System.out.println("Last element: " + deque.getLast());
    System.out.println("=== dequeList === ");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);

    System.out.println("Removed first element: " + deque.removeFirst());
    System.out.println("Remove last element: " + deque.removeLast());
    System.out.println("=== dequeList === ");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);

    System.out.println("Deque size: " + deque.size());
    System.out.println("Is deque empty? " + deque.isEmpty());
    System.out.println("=== dequeList === ");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);
  }
}

/*
=== 좜λ ₯ ===
=== dequeList === 
[0, 1, 2, 3]
First element: 0
Last element: 3
=== dequeList === 
[0, 1, 2, 3]
Removed first element: 0
Remove last element: 3
=== dequeList === 
[1, 2]
Deque size: 2
Is deque empty? false
=== dequeList === 
[1, 2]
*/

4️⃣ μž…λ ₯ μ œν•œ Deque(Input-Restricted Deque).

μž…λ ₯ μ œν•œ Deque(Input-Restricted Deque)은 덱의 ν•œμͺ½ λμ—μ„œλ§Œ μ‚½μž…μ΄ κ°€λŠ₯ν•˜κ³ , μ–‘μͺ½ λμ—μ„œ μ‚­μ œκ°€ κ°€λŠ₯ν•œ μžλ£Œκ΅¬μ‘°μž…λ‹ˆλ‹€.

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class InputRestrictedDeque<E> {
  private static final int DEFAULT_CAPACITY = 10;
  private E[] elements;
  private int head;
  private int tail;
  private int size;

  @SuppressWarnings("unchecked")
  public InputRestrictedDeque() {
    elements = (E[]) new Object[DEFAULT_CAPACITY];
    head = 0;
    tail = 0;
    size = 0;
  }

  public void addLast(E e) {
    if (size == elements.length) {
      resize();
    }
    elements[tail] = e;
    tail = (tail + 1) % elements.length;
    size++;
  }

  public E removeFirst() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    E element = elements[head];
    elements[head] = null; // for garbage collection
    head = (head + 1) % elements.length;
    size--;
    return element;
  }

  public E removeLast() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    tail = (tail - 1 + elements.length) % elements.length;
    E element = elements[tail];
    elements[tail] = null; // for gatbage collection
    size--;
    return element;
  }

  public E getFirst() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    return elements[head];
  }

  public E getLast() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    return elements[(tail - 1 + elements.length) % elements.length];
  }

  public int size() {
    return size;
  }

  public boolean isEmpty() {
    return size == 0;
  }

  private void resize() {
    int newCapacity = elements.length * 2;
    @SuppressWarnings("unchecked")
    E[] newElements = (E[]) new Object[newCapacity];
    for (int i = 0; i < size; i++) {
      newElements[i] = elements[(head + i) % elements.length];
    }
    elements = newElements;
    head = 0;
    tail = size;
  }

  public ArrayList<E> toArrayList() {
    return IntStream.range(0, size)
                    .mapToObj(i -> elements[(head + i) % elements.length])
                    .collect(Collectors.toCollection(ArrayList::new));
  }
}

// Main
import java.util.ArrayList;

public class Main {

  public static void main(String[] args) {
    InputRestrictedDeque<Integer> deque = new InputRestrictedDeque<>();
    deque.addLast(1);
    deque.addLast(2);
    deque.addLast(3);

    ArrayList<Integer> dequeList = deque.toArrayList();
    System.out.println("=== dequeList ===");
    System.out.println(dequeList);

    System.out.println("First element: " + deque.getFirst());
    System.out.println("Last element: " + deque.getLast());
    System.out.println("=== dequeList ===");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);

    System.out.println("Remove first element: " + deque.removeFirst());
    System.out.println("Remove last elment: " + deque.removeLast());
    System.out.println("=== dequeList ===");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);

    System.out.println("Deque size: " + deque.size());
    System.out.println("Is deque empty? " + deque.isEmpty());
  }
}
/*
=== 좜λ ₯ ===
=== dequeList ===
[1, 2, 3]
First element: 1
Last element: 3
=== dequeList ===
[1, 2, 3]
Remove first element: 1
Remove last elment: 3
=== dequeList ===
[2]
Deque size: 1
Is deque empty? false
*/

1️⃣ μ½”λ“œ μ„€λͺ….

  1. λ°°μ—΄ μ΄ˆκΈ°ν™” : DEFAULT_CAPACITY 크기의 배열을 μ΄ˆκΈ°ν™”ν•˜κ³ , head, tail, size λ³€μˆ˜λ₯Ό μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.

  2. μ‚½μž… μ—°μ‚°(addLast) : μš”μ†Œλ₯Ό 덱의 λ§ˆμ§€λ§‰ 에 μΆ”κ°€ν•©λ‹ˆλ‹€. 배열이 가득 μ°¨λ©΄ 크기λ₯Ό 두 배둜 λŠ˜λ¦½λ‹ˆλ‹€.

  3. μ‚­μ œ μ—°μ‚°(removeFirst, removeLaste) : 첫 번째 μš”μ†Œμ™€ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό 각각 μ œκ±°ν•©λ‹ˆλ‹€.

  4. 쑰회 μ—°μ‚°(getFirst, getLast) : 첫 번째 μš”μ†Œμ™€ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

  5. 기타 λ©”μ„œλ“œ : size 와 isEmpty λ©”μ„œλ“œλŠ” 덱의 크기와 덱이 λΉ„μ–΄ μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

  6. λ°°μ—΄ 크기 μ‘°μ • (resize) : 배열이 가득 μ°° λ•Œ 호좜되며, λ°°μ—΄μ˜ 크기λ₯Ό 두 배둜 늘리고 μš”μ†Œλ₯Ό μƒˆ λ°°μ—΄λ‘œ λ³΅μ‚¬ν•©λ‹ˆλ‹€.

이 μ˜ˆμ œμ—μ„œλŠ” μš”μ†Œλ₯Ό 덱의 λμ—λ§Œ μ‚½μž…ν•  수 μžˆλŠ” μž…λ ₯ μ œν•œ 덱을 κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.
ν•„μš”μ— 따라 이 κ΅¬ν˜„μ„ ν™•μž₯ν•˜κ±°λ‚˜ μˆ˜μ •ν•˜μ—¬ μš”κ΅¬μ‚¬ν•­μ— 맞게 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

5️⃣ 좜λ ₯ μ œν•œ Deque(Output-Restricted Deque).

좜λ ₯ μ œν•œ Deque(Output-Restricted Deque)은 μ–‘μͺ½ λμ—μ„œ μ‚½μž…μ΄ κ°€λŠ₯ν•˜μ§€λ§Œ, ν•œμͺ½ λμ—μ„œλ§Œ μ‚­μ œκ°€ κ°€λŠ₯ν•œ 자료 κ΅¬μ‘°μž…λ‹ˆλ‹€.

이 κ΅¬μ‘°λŠ” μ–‘μͺ½ λμ—μ„œ μš”μ†Œλ₯Ό μΆ”κ°€ν•  수 μžˆμ§€λ§Œ, μ‚­μ œλŠ” ν•œμͺ½ λμ—μ„œλ§Œ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class OutputRestrictedDeque<E> {
  private static final int DEFAULT_CAPACITY = 10;
  private E[] elements;
  private int head;
  private int tail;
  private int size;

  @SuppressWarnings("unchecked")
  public OutputRestrictedDeque() {
    elements = (E[]) new Object[DEFAULT_CAPACITY];
    head = 0;
    tail = 0;
    size = 0;
  }

  public void addFirst(E e) {
    if (size == elements.length) {
      resize();
    }
    head = (head - 1 + elements.length) % elements.length;
    elements[head] = e;
    size++;
  }

  public void addLast(E e) {
    if (size == elements.length) {
      resize();
    }
    elements[tail] = e;
    tail = (tail + 1) % elements.length;
    size++;
  }

  public E removeFirst() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    E element = elements[head];;
    elements[head] = null; // for garbage collection
    head = (head + 1) % elements.length;
    size--;
    return element;
  }

  public E getFirst() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    return elements[head];
  }

  public E getLast() {
    if (size == 0) {
      throw new NoSuchElementException();
    }
    return elements[(tail - 1 + elements.length) % elements.length];
  }

  public int size() {
    return size;
  }

  public boolean isEmpty() {
    return size == 0;
  }

  private void resize() {
    int newCapacity = elements.length * 2;
    @SuppressWarnings("unchecked")
    E[] newElements = (E[]) new Object[newCapacity];
    for (int i = 0; i < size; i++) {
      newElements[i] = elements[(head + 1) % elements.length];
    }
    elements = newElements;
    head = 0;
    tail = size;
  }

  public ArrayList<E> toArrayList() {
    return IntStream.range(0, size)
                    .mapToObj(i -> elements[(head + i) % elements.length])
                    .collect(Collectors.toCollection(ArrayList::new));
  }
}

// Main
import java.util.ArrayList;

public class Main {

  public static void main(String[] args) {
    OutputRestrictedDeque<Integer> deque = new OutputRestrictedDeque<>();
    deque.addFirst(1);
    deque.addLast(2);
    deque.addFirst(0);
    deque.addLast(3);

    ArrayList<Integer> dequeList = deque.toArrayList();
    System.out.println("=== dequeList === ");
    System.out.println(dequeList);

    System.out.println("First element: " + deque.getFirst());
    System.out.println("Last element: " + deque.getLast());
    System.out.println("=== dequeList === ");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);

    System.out.println("Remove first element: " + deque.removeFirst());
    System.out.println("=== dequeList === ");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);

    System.out.println("Deque size: " + deque.size());
    System.out.println("Is deque empty? " + deque.isEmpty());
    System.out.println("=== dequeList === ");
    dequeList = deque.toArrayList();
    System.out.println(dequeList);
  }
}

/*
=== 좜λ ₯ ===
=== dequeList ===
[0, 1, 2, 3]
First element: 0
Last element: 3
=== dequeList ===
[0, 1, 2, 3]
Remove first element: 0
=== dequeList ===
[1, 2, 3]
Deque size: 3
Is deque empty? false
=== dequeList ===
[1, 2, 3]
*/

1️⃣ μ½”λ“œ μ„€λͺ….

  1. λ°°μ—΄ μ΄ˆκΈ°ν™” : DEFAULT_CAPACITY 크기의 배열을 μ΄ˆκΈ°ν™”ν•˜κ³ , head, tail, size λ³€μˆ˜λ₯Ό μ΄ˆκΈ°ν™” ν•©λ‹ˆλ‹€.

  2. μ‚½μž… μ—°μ‚°(addFirst, addLast) : μš”μ†Œλ₯Ό 덱의 첫 번째 λ˜λŠ” λ§ˆμ§€λ§‰μ— μΆ”κ°€ν•©λ‹ˆλ‹€. 배열이 가득 μ°¨λ©΄ 크기λ₯Ό 두 배둜 λŠ˜λ¦½λ‹ˆλ‹€.

  3. μ‚­μ œ μ—°μ‚°(removeFirst) : 첫 번째 μš”μ†Œλ₯Ό μ œκ±°ν•©λ‹ˆλ‹€. 좜λ ₯ μ œν•œ λ±μ—μ„œλŠ” 첫 번째 μš”μ†Œλ§Œ μ œκ±°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  4. 쑰회 μ—°μ‚°(getFirst, getLast) : 첫 번째 μš”μ†Œμ™€ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

  5. 기타 λ©”μ„œλ“œ : size 와 isEmpty λ©”μ„œλ“œλŠ” 덱의 크기와 덱이 λΉ„μ–΄ μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

  6. λ°°μ—΄ 크기 μ‘°μ •(resize) : 배열이 가득 μ°° λ•Œ 호좜되며, λ°°μ—΄μ˜ 크기λ₯Ό 두 배둜 늘리고 μš”μ†Œλ₯Ό μƒˆ λ°°μ—΄λ‘œ λ³΅μ‚¬ν•©λ‹ˆλ‹€.

이 μ˜ˆμ œμ—μ„œλŠ” μš”μ†Œλ₯Ό 덱의 μ–‘μͺ½ λμ—μ„œ μ‚½μž…ν•  수 있고, 첫 번째 μš”μ†Œλ§Œ μ œκ±°ν•  수 μžˆλŠ” 좜λ ₯ μ œν•œ 덱을 κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.
ν•„μš”μ— 따라 이 κ΅¬ν˜„μ„ ν™•μž₯ν•˜κ±°λ‚˜ μˆ˜μ •ν•˜μ—¬ μš”κ΅¬μ‚¬ν•­μ— 맞게 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.