【发布时间】:2016-01-24 08:23:20
【问题描述】:
我的问题是在我的主要方法中,如何将多个节点添加到链表中......我现在首先拥有的是 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);
如果以上设置了这些值,我如何将它们全部添加?
然后我需要为每一项设置数据和下一步吗? (这似乎是多余的,因为我似乎在上面的构造函数中设置了每个节点 data 和 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();
}
}
我没有添加我的其他两个课程,因为我认为这无关紧要,但如果您想看到它,请告诉我。我很新,这只是我的第二节课,它是在线的,而且课程结构很差,请多多关照,哈哈
单链表类
//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 开头设置的 SinglyLinkedList 对象来添加这些节点?
-
我还是有点迷茫,我要贴出我的 SinglyLinkedList 类给你看看你能不能告诉我应该用什么方法来添加这些节点?我唯一能想到的是 addFirst() 方法,但是如何添加第一个节点之后的节点?这有意义吗?
-
我明白了,这是有道理的,但我现在对 LinkedLists 最困惑的部分是如何添加多个节点。我看过的视频显示了像我这样的例子,我看到了如何设置值的结构以及下一个节点将是什么,但我实际上并没有添加这些值,我不知道如何添加。我想添加这些节点,这样我就可以更轻松地测试我编写的所有其他方法。
-
我已将我的 cmets 移入答案以进行清理。
标签: java linked-list nodes implementation