在LinkedList

时间:2015-10-25 01:53:08

标签: java

public class LinkedList<T>
{
    private Node head;
    private int size;
    public LinkedList()
    {

    }
    public void addToHead(T value) // create new node, make new node point to head, and head point to new node
    {
        if (head == null)
        {
            head = new Node(value,null);
        }
        else
        {
            Node newNode = new Node(value,head);
            head = newNode;
        }
        size++;
    }
    public boolean isEmpty()
    {
        return head == null;
    }
    public int size()
    {
        return size;
    }
    public void removeHead()
    {
        head = head.next;
        size--;
    }
    public void addToTail(T value)
    {
        if (isEmpty())
        {
            System.out.println("You cannot addtoTail of a emptyList!");
        }
        else
        {
            System.out.println(value);
            Node current = head;
            System.out.println("we are pointing to head: "+current);
            while (current.getNext() != null) // loop till the end of the list (find the last node)
            {
                System.out.println("we are now pointing to: "+current.getElement());
                current = current.getNext();
            } 

            System.out.println("We are at the last node:"+current); // its working
            System.out.println("it should point to null:"+current.getNext()); // its working
            current.setNext(new Node(value,null)); // make it point to our new node we want to insert
            System.out.println(current.getNext()); // it is pointing to the new node.. yet the node is not actually inserted (local variable problem? )
            size++;
        }

    }
    public String toString()
    {
        String output = "";
        if (!isEmpty())
        {
            Node current = head;
            output = "";
            while (current.getNext() != null)
            {
                output += current.toString()+ "->";
                current = current.getNext();
            }
        }
        return output;
    }
    protected class Node
    {
        private T element;
        private Node next;
        public Node()
        {
            this(null,null);
        }
        public Node(T value, Node n)
        {
             element = value;
             next = n;
        }
        public T getElement()
        {
            return element;
        }
        public Node getNext()
        {
            return next;
        }
        public void setElement(T newElement)
        {
            element = newElement;
        }
        public void setNext(Node newNext)
        {
            next = newNext;
        }
        public String toString()
        {
            return ""+element;
        }
    }
}

所以我编写了这个linkedlist类,除了addtoTail之外,每个方法都有效。例如,假设我创建了一个我的linkedlist类的实例,并调用addToHead(5),然后调用addtoTail(6)并使用我的toString方法打印出链表,它只包含5-&gt;。我调试了addToTail,一切似乎都指向了正确的位置,但由于某种原因,它没有将新节点(6)添加到列表中。希望我能清楚地解释清楚。我可能遗漏了一些非常简单的东西(我甚至把它画在纸上以形象化,但没有看到问题)。

1 个答案:

答案 0 :(得分:1)

您的addToTail功能可能还不错。我认为罪魁祸首是你的toString功能。特别是在这个片段中:

while (current.getNext() != null)
{
    output += current.toString()+ "->";
    current = current.getNext();
}

您的情况在到达结束前终止循环。你真正想要的是:

while(current != null) {
    ....
}
相关问题