public class Node {
public int data; public Node next;
public Node0 { next = null; }
带附设头结点的单链表类List定义如下:
public class List {
private Node head;
public ListO { head = new Node; }
}
编写List类中的getLengthO方法,返回单链表的长度。
参考如下:
public class List {
private Node head;
public List() { head = new Node; }
public int getLength() {
int length = 0;
Node temp = head;
while (null != temp) {
length++;
temp = temp.next;
}
return length;
}
}