[英]Java LinkedList adding multiple nodes
我的问题是在我的主要方法中,如何将多个节点添加到链接列表中。...我现在首先拥有的是node2,node3 ..我以为正在添加这些节点,但我意识到我不认为自己对这些节点及其值做任何事情,对不对? 如何使用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);
如果以上设置了值,如何将它们全部添加?
然后是否需要设置数据,然后为每个数据设置下一步? (这似乎是多余的,因为我似乎要在上面的构造函数中设置每个节点数据和next的值?)
first.setData("first");
first.setNext(node2);
node2.setData("Second");
node2.setNext(node2);
//.....
我试图添加上述所有节点,以便可以通过添加新节点来测试addLast()方法。 但是,当我在main中调用我的addLast()方法时,您可以看到,下面唯一打印的是我添加的addLast()值(如果我调用addFirst(),则是第一个)。
测试班
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();
}
}
我没有添加其他两个类,因为我不认为这是相关的,但是如果您希望看到它,请告诉我。 我很新,这只是我的第二堂课,并且它是在线上并且教学质量不佳,请大声笑
SinglyLinkedList类
//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类是您的基本getNext setNext,getData setData ....
有两点,主要是总结评论:
您根本不应该在main()
中使用ListNode
对象-这应该是SinglyLinkedList
类的工作。 ListNode
甚至不需要在其余代码中可见,它可以是SinglyLinkedList
的嵌套类。 您只应与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()
,效果相同。 如果要从一些基础数据开始测试其他方法,则可以在主类中创建一些辅助方法以这种方式填充列表。
附带说明:如果printList()
将java.io.PrintStream out
用作参数,则应使用它而不是System.out
。 那是
out.println(...)
代替
System.out.println(...)
另外,最好抛出NoSuchElementException
而不是NullPointerException
,以表明所请求的元素不存在。
如果您想方便地填充列表,则可以在主类中添加以下内容:
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");
你想做什么? 如果您尝试填充链表,则只需连续调用list.addLast,它将使用一个参数(要添加的新节点中的数据),并处理创建新节点和将其放在列表的后面。
我假设您不需要在主节点中创建任何节点,因为它们通常完全由链表类处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.