2011-12-02 5 views
0

私は現在、大学のJavaコースのDoubly Linked Listプロジェクトに取り組んでいます。私は、二重リンクリスト、リンクリスト、リストという概念を理解しています。しかし私は私の方法で変更する必要があるデータを作成する方法がわからないので、私のプログラムを書くのに苦労しています。私たちの教授は、通常は彼が使用する入力を与えましたが、今回はできませんでした。私の研究ではそれを理解できないようです。DoublyLinkedListプロジェクト:入力例

私の主な質問は、誰かが私の作業を開始し、私の方法がより良くなる必要があることを理解し始めるためのコードを書くことができると思いますか?

これまで私がこれまで持っていたことは次のとおりです。 (基本的にオーバーライドスケルトン..)

ありがとうございました。

import java.util.Collection; 
import java.util.Iterator; 
import java.util.List; 
import java.util.ListIterator; 

public class DoublyLinkedList<E> implements List<E>{ 

DoublyLinkedListNode header; 

public static void main(String[] args) { 

} 
public boolean add(E e) { 
    return false; 
} 
public void add(int index, E element) { 

} 
public boolean addAll(Collection<? extends E> c) { 
    return false; 
} 
public void clear() { 
    header=null; 
} 
public boolean contains(Object o) { 
    return false; 
} 
public E get(int index) { 
    return null; 
} 
public int indexOf(Object o) { 
    return 0; 
} 
public boolean isEmpty() { 
    return header == null; 
} 
public int lastIndexOf(Object o) { 
    return 0; 
} 
public ListIterator<E> listIterator() { 
    return null; 
} 
public boolean remove(Object o) { 
    return false; 
} 
public E remove(int index) { 
    return null; 
} 
public int size() { 
    return 0; 
} 
public Object[] toArray() { 
    return null; 
} 
private class DoublyLinkedListNode{ 
    DoublyLinkedListNode next; 
    DoublyLinkedListNode last; 
    E contents; 
} 

//extra credit 
private class DoublyLinkedListItr implements java.util.ListIterator{ 

    public void add(Object arg0) { 

    } 
    public boolean hasNext() { 

     return false; 
    } 
    public boolean hasPrevious() { 

     return false; 
    } 
    public Object next() { 

     return null; 
    } 
    public int nextIndex() { 

     return 0; 
    } 
    public Object previous() { 

     return null; 
    } 
    public int previousIndex() { 

     return 0; 
    } 
    public void remove() { 

    } 
    public void set(Object arg0) { 

    } 

} 
public ListIterator<E> listIterator(int index) { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public <T> T[] toArray(T[] a) { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public List<E> subList(int fromIndex, int toIndex) { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public boolean retainAll(Collection<?> c) { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public E set(int index, E element) { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public boolean removeAll(Collection<?> c) { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public boolean addAll(int index, Collection<? extends E> c) { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public Iterator<E> iterator() { 
    throw new UnsupportedOperationException("not implemented"); 
} 
public boolean containsAll(Collection<?> c) { 
    throw new UnsupportedOperationException("not implemented"); 
} 

}

+0

あなたは本当に 'List'インタフェースを実装する必要がありますか? – havexz

答えて

0

は抜粋です:

public static void main(String[] args) { 
    DoublyLinkedList<String> doublyLinkedList = new DoublyLinkedList<String>(); 
    doublyLinkedList.add("Hello"); 
    doublyLinkedList.add("World"); 
    // If you want to store int 
    DoublyLinkedList<Integer> dlli = new DoublyLinkedList<Integer>(); 
    dlli.add(new Integer(10)); 
    dlli.add(new Integer(5)); 
} 

私は、これはあなたが探しているものであると思います。

+0

完璧、ありがとう! – user1078174

0

ノードを作成し、その中に値を格納します。

ヘッダーがnullの場合は、ヘッダーに新しいノードを参照させます。

headerがnullでない場合は、次の値がNULLでない限り、nextで参照されるノードを取得します。 nextがnullの場合は、リストの最後にあり、次の参照を取得し、新しく作成したノードを参照させます。次に、新しいノードの最後の参照(前に呼び出されたと思います)を参照し、見つかったノード(リストの末尾)を参照させます。

これは、あなたを始めてくれるはずです。ここでデータを作成するための

関連する問題