2011-12-02 27 views
0

我目前的工作在我的大學課程的Java雙向鏈表項目。我瞭解雙鏈表,鏈表和鏈表的概念。但是我在編寫程序時遇到了很多麻煩,因爲我不確定如何創建需要在我的方法中修改的數據。我們的教授通常會給我們他將要使用的輸入,但這次沒有,我似乎無法在我的研究中發現它。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

創建節點和所述值存儲在它。

如果標題爲空,有標題引用您的新節點。

如果標頭不爲空,則取指向的節點,並且只要next不爲空,則取下一個引用的節點。如果next爲null,則位於列表的末尾,請參考下一個參考,並讓它引用新創建的節點。然後把最後一個(我以前稱之爲)新節點的引用,並讓它引用你找到的節點(列表的末尾)。

應該讓你開始那。