0

に非常に短いコードdequeを書かなければなりませんが、メソッドのコードを書く方法がわかりません。もし誰かがメソッドの1つを手伝ってくれるなら(例えば、オブジェクトを追加するメソッド) deque の from に)、それで始められます。残りのメソッドを管理できると確信していますが、現時点ではかなり困惑しています。

4

2 に答える 2

6

通常、deque は二重にリンクされたリストとして実装されます。リスト内の最初と最後の要素を追跡し、各要素がその先行要素と後続要素を追跡できるようにすることで、双方向リンク リストを実装します。

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;
        }
    }
}
于 2009-08-08T14:35:42.817 に答える
2

あなたが何を求めているのか正確にはわかりませんが、Deque で使用可能なメソッドはJavadocにリストされています

于 2009-08-08T14:32:08.393 に答える