【发布时间】:2016-01-24 07:25:17
【问题描述】:
我现在正在学习链接列表,我有点困惑。我正在处理一项任务,我们正在创建自己的一些方法的实现,以与链表一起使用。
我观看了很多关于链接列表概念的视频,以及节点如何工作,例如获取数据和设置下一个元素,但我对如何制作列表来测试我正在实施的方法感到困惑。我知道这听起来可能很愚蠢,但我开始对方法中的多个参数感到非常困惑,并且涉及到返回语句,而且我之前从未使用过通用数据类型,所以这些基本想法可能会让我感到困惑。
我希望在创建列表方面得到一些帮助,以便我可以在创建方法时对其进行测试,但我在打印列表时遇到了困难。我知道我主要做的是创建新节点并添加到一些列表中,但老实说,我不知道这个所谓的 LinkedList 在哪里或它叫什么。任何帮助是极大的赞赏!!仅供参考,我对 Java 很陌生,这是我的第二堂课,它是在线的,不是一个很好的课程,请多多关照,哈哈
我明白了这个概念,但是这些被添加到的列表(或链接列表)的名称是什么?
ListNode node4 = new ListNode("Fourth", null);
ListNode node3 = new ListNode("Third", node4);
ListNode node2 = new ListNode("Second", node3);
ListNode node1 = new ListNode("First", node2);
我不知道如何像上面那样单独打印这些,因为我可以使用上面的名字
ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));
这是我的测试类代码,其中我有问题作为 cmets,如果我在上面没有意义,这可能会帮助您理解我对什么感到困惑。我省略了 ListNode 类,因为它是您的基本 getNext() setNext() Node 类,称为 ListNode ,具有泛型类型......
//Testing program for SinglyLinkedList 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
SinglyLinkedList SLL = new SinglyLinkedList();
ListNode node4 = new ListNode("Fourth", null);
ListNode node3 = new ListNode("Third", node4);
ListNode node2 = new ListNode("Second", node3);
ListNode node1 = new ListNode("First", node2);
//ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));
System.out.println(node2.getData());
//Is this the correct way to add a new node to this method?
SLL.addLast(new ListNode("Fifth", null));
//I doubt my printList is correct as I do not know what parameter I am supposed to pass to it.
SLL.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() {
}
}
【问题讨论】:
-
您能否在代码之外发布您的问题?看不懂,很难找到。
-
我的问题用粗体表示,但我会将这些 cmets 取出并将它们与代码之外的那部分一起添加,以免混淆
标签: java linked-list implementation singly-linked-list