Java LinkedList 添加多个节点

Java LinkedList adding multiple nodes

我的问题是在我的主要方法中,如何将多个节点添加到链表中....我现在首先拥有的是节点 2、节点 3 ..我以为是在添加这些节点,但我意识到我没有别以为我真的在用这些节点和它们的值做任何事情,对吧?我如何使用 setData() 和 setNext() 添加所有这些节点。那有意义吗?

ListNode<String> node4 = new ListNode<String>("Fourth", null);
ListNode<String> node3 = new ListNode<String>("Third", node4);
ListNode<String> node2 = new ListNode<String>("Second", node3);
ListNode<String> first = new ListNode<String>("First", node2);

如果以上设置了值,我该如何将它们全部相加?

然后我需要为其中的每一个设置数据和下一步吗? (这似乎是多余的,因为我似乎在上面的构造函数中设置了每个节点数据和下一个节点的值?)

first.setData("first");
first.setNext(node2); 
node2.setData("Second");
node2.setNext(node2);
//.....

我正在尝试添加上述所有节点,以便我可以通过添加新节点来测试我的 addLast() 方法。但是,当我在 main 中调用我的 addLast() 方法时,如下所示,唯一打印的是我添加的 addLast() 值(如果我调用 addFirst() 则首先打印)。

测试Class

public class LinkedListDriver
{

    public static void main(String[] args) {
        //List<String> list = new LinkedList<String>();                   //comment out this line to test your code
        SinglyLinkedList<String> list = new SinglyLinkedList<String>(); //remove comment to test your code

        ListNode<String> node4 = new ListNode<String>("Fourth", null);
        ListNode<String> node3 = new ListNode<String>("Third", node4);
        ListNode<String> node2 = new ListNode<String>("Second", node3);
        ListNode<String> first = new ListNode<String>("First", node2);

        ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null)));

        //I've been messing around with this but 
        list.addFirst(first.getData());
        list.addFirst("Second");

        list.addLast("Fifth");
        list.printList();
    }
}

我没有添加其他两个 class 是因为我认为它不相关,但如果你想看它,请告诉我。我是新手,这只是我的第二个 class,它在线并且构造不佳 class,请保持友善哈哈

单链表class

//This class implements a very simple singly-linked list of Objects
public class SinglyLinkedList<E>
{
    ListNode<E> first; // first element

    public SinglyLinkedList() {
        first = null;
    }

    public E getFirst() {
        if (first == null) {
            throw new NoSuchElementException();
        } else
            return first.getData();
    }

    public void addFirst(E value) {
        first = new ListNode<E>(value, first);
    }

    // Methods below implemented by you. Note: while writing methods, keep in mind
    // that you might be able to call other methods in this class to help you - you
    // don't always need to start from scratch(but you'll have to recognize when)

    public void addLast(E value) {
        ListNode<E> temp = first;
        //If list is empty make new node the first node.
        if (temp == null) {
            first = new ListNode <E>(value, null);
            first.setNext(null);
        }//Otherwise loop to end of list and add new node.
        else {
            while (temp.getNext() != null) {
                temp = temp.getNext();
            }
            temp.setNext(new ListNode<E>(value, null));
        }
    }//end addLast

    // throws an exception - you decide when and which one

    public E getLast() {
        ListNode<E> temp = first;
        if (temp == null) {
            throw new NullPointerException("There are no elements in this list to get.");
        } else {
            while (temp.getNext() != null) {
                temp = temp.getNext();
            }
            return temp.getData();
        }
    }

    // throws an exception - you decide when and which one

    public E removeFirst() {
        if (first == null) {
            throw new NullPointerException("There are no elements in this list to remove.");
        }
        ListNode<E> tempRemove = first;
        return null; //just so it'll compile
    }

    // throws an exception - you decide when and which one

    public E removeLast() {
        return null; //just so it'll compile
    }

    // return the number of elements in the list

    public int size() {
        return 0; //just so it'll compile
    }

    // return true if o is in this list, otherwise false

    public boolean contains(E obj) {
        return true; //just so it'll compile
    }

    public void printList(java.io.PrintStream out) {
        if (first == null) {
            System.out.println("The list is empty");
        }
        ListNode<E> current = first;
        while (current != null) {
            System.out.println(current.toString());
            current = current.getNext();
        }
    }

    public String toString() {
        String s = "[";
        ListNode<E> current = first;
        //write code to traverse the list, adding each object on its own line
        while (current.getNext() != null) {
            current = current.getNext();
        }

        s += "]";
        return s;
    }

    // OPTIONAL: just for fun...and a challenge

    public void reverse() {
    }
}

ListNode class 是你的基本getNext setNext, getData setData....

你想做什么?如果您尝试填充链表,您需要做的就是不断调用 list.addLast,它将采用单个参数(您要添加的新节点中的数据),并处理创建新节点节点并将其放在列表的后面。

我假设您不需要在主节点中创建任何节点,因为它们通常完全由链表处理 class。

几点,主要是对评论的总结:

您根本不应该使用 main() 中的 ListNode 个对象 - 那应该是 SinglyLinkedList class 的工作。 ListNode 甚至不需要对其余代码可见,它可以是 SinglyLinkedList 中的嵌套 class。您应该只与 SinglyLinkedList.

交换数据对象(在本例中为字符串)

如果您想测试 addLast() 方法,您可以从一个空列表开始并重复调用 list.addLast(),正如 Shane 在他的回答中提到的那样。这样你就可以确保它在列表为空和非空时都能正常工作。

SinglyLinkedList<String> list = new SinglyLinkedList<String>();
list.addLast("first");
list.addLast("second");
list.addLast("third");
list.printList(System.out);

至于在一次调用中添加多个节点 - 此链表没有相应的方法。例如,您可以添加一个方法来添加一个数组的所有元素,但您也可以按顺序调用 addLast() 来获得相同的效果。如果你想从一些基础数据开始测试其他方法,你可以在主 class 中创建一些辅助方法来填充列表。

附带说明:如果 printList()java.io.PrintStream out 作为参数,您应该使用它而不是 System.out。即

out.println(...)

而不是

System.out.println(...)

此外,最好抛出 NoSuchElementException 而不是 NullPointerException 作为请求元素不存在的指示。

如果你想要一种方便的方式来填充列表,你可以在主 class:

中有这样的东西
static <E> void addToList(SinglyLinkedList<E> list, E... values) {
    for (E value : values) {
        list.addLast(value);
    }
}

并像这样使用它:

SinglyLinkedList<String> list = new SinglyLinkedList<String>();
addToList(list, "first", "second", "third");