插入通用双链表

时间:2014-06-16 02:05:00

标签: java generics

我无法围绕我负责创建的双向链表的通用方面进行思考。我不知道如何测试我的方法的正确性,所以任何输入都会非常感激。

 public class MyLinkedList<E> implements List<E> 
{
    //Instance variables
    int size;
    Node head;
    Node tail;

    /**
     * Constructor.  Creates a blank linked list.
     */
    public MyLinkedList() 
    {
        MyLinkedList<E> blankList = new MyLinkedList<E>();
    }

    /**
     * @param element - The element to add at the beginning of the list.
     *  
     * Inserts the specified element at the beginning of the list.
     * O(1) for a doubly-linked list.
     */
    public void addFirst(E element) 
    {
        Node firstPosition = head;
        Node newNode = new Node(element);
        newNode.next = firstPosition;
        firstPosition = newNode;

        if(firstPosition == null)
        {
            head = newNode;
        }
        else
        {
            firstPosition.prev = newNode;
        }
}

private class Node 
{       
    E data;
    Node next;
    Node prev;

    public Node(E element)
    {
        //TODO          
    }       
}

0 个答案:

没有答案