Java驱动程序类不使用linkedList

时间:2017-03-20 02:16:01

标签: java data-structures

我的驱动程序中的第10行和我的linkedList类中的第19行不能一起使用。我找不到问题,但我认为这与宣言有关。有什么帮助吗?

public class Driver 
{
    public static void main(String[] args)
    {
        LinkedList linkedList = new LinkedList();

        linkedList.add(3);
        linkedList.add(7);
        linkedList.add(12);

        linkedList.printList();

        linkedList.add(2);
        linkedList.add(6);
        linkedList.add(10);
        linkedList.add(13);

        linkedList.printList();
    }
}

和linkedList类

package part2;

public class LinkedList 
{
    Node head;
    int size;

    public LinkedList()
    {
        head = null;
        System.out.println("LinkedList created...");
    }

    public void add (int data)
    {
        Node current = head;
        Node newNode = new Node (data);

        while(current.next != null && current.next.data < newNode.data)
        {
            current = current.next;
        }

        newNode.next = current.next;
        current.next = newNode;
        System.out.println("LinkedList.add(), element: " +data+ "added. Element # "+size+ "...");

    }

    public void printList()
    {
        Node current = head;
        while(current.next != null)
        {
            System.out.println("LinkedList.printList(), element: " +current.data + "...");
            current = current.next;
        }
        System.out.println("LinkedList.printList(), element: " +current.data + "...");
    }

    public void delete( int dataToRemove)
    {
        for(Node current = head; current.next != null; current = current.next)
        {
            if (current.next.equals(dataToRemove))
            {
                current.next = current.next.next;
            }
        }
    }

    private class Node
    {

        public Node(int data)
        {
            this.data= data;
            this.next = null;
        }

        Node next;
        int data;
    }
}

1 个答案:

答案 0 :(得分:1)

添加第一个节点时,需要初始化头节点。否则头节点始终为空。例如:

public class LinkedList {
    Node head;
    int size;

    public LinkedList() {
        head = null;
        System.out.println("LinkedList created...");
    }

    public void add(int data) {
        Node current = head;
        Node newNode = new Node(data);

        size++;

        if (head == null) {
            // Init the head node first
            head = newNode;
            System.out.println("LinkedList.add(), element: " + data + "added. Element # " + size + "...");
            return;
        }

        while (current.next != null && current.next.data < newNode.data) {
            current = current.next;
        }

        newNode.next = current.next;
        current.next = newNode;
        System.out.println("LinkedList.add(), element: " + data + "added. Element # " + size + "...");

    }

    public void printList() {
        Node current = head;
        while (current.next != null) {
            System.out.println("LinkedList.printList(), element: " + current.data + "...");
            current = current.next;
        }
        System.out.println("LinkedList.printList(), element: " + current.data + "...");
    }


    public void delete(int dataToRemove) {
        for (Node current = head; current.next != null; current = current.next) {
            if (current.next.data == dataToRemove) {
                current.next = current.next.next;
                size--;
                break;
            }
        }
    }

你的删除方法也不正确。您需要将数据值与给定数据进行比较,而不是将节点与给定数据进行比较。我也为此做了改变。