2009-08-08 1 views
0

deque에 매우 짧은 코드를 작성해야하지만 누군가 메소드 중 하나를 사용하면 메소드에 대한 코드를 작성하는 방법을 알 수 없습니다 (예 : 메소드 deque에서 객체를 추가하십시오.) 그러면 저를 시작할 수 있습니다. 나머지 메소드를 관리 할 수있을 것이라고 확신합니다. 지금 당황 스럽습니다.LinkedList와 같은 기존 클래스를 사용하지 않고 Java Deque를 사용할 수 있습니까?

+0

왜이 작업을 수행하고 있습니까? 표준 Deque 구현 중 하나를 사용하는 것이 잘못된 이유는 무엇입니까? –

답변

2

난 후 당신 정확히 모르겠지만, 양단 큐에 사용할 수있는 방법은 일반적으로 이중 연결리스트로 구현되는 Javadoc

6

Deques에 나열되어 있습니다. 목록의 첫 번째 요소와 마지막 요소를 추적하고 각 요소가 선행 및 후계를 추적하도록하여 이중 연결 목록을 구현합니다.

public class Deque<T> { 
    private class Node { 
     Node(T value) { 
      this.value = value; 
     } 
     T value; 
     Node next, prev; 
    } 

    private Node first, last; 

    public void addFront(T value) { 
     Node oldFirst = first; 
     first = new Node(value); 

     // The old first item is now the second item, so its the successor of 
     // the new first item 
     first.next = oldFirst; 

     // if first was null before, that means the deque was empty 
     // so first and last should both point to the new item 
     if(oldFirst == null) { 
      last = first; 
     } else { 
      // If there previously was a first element, this element is 
      // now the second element and its prev field should point to 
      // the new first item 
      oldFirst.prev = first; 
     } 
    } 
} 
관련 문제