单向链表的实现
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
public class SinglyList<T> implements Iterable<T> {
//记录头结点
private Node head;
//记录链表长度
private int L;
//创建一个结点类的匿名内部类
private class Node {
//数据域,存储数据
T data;
//地址域,存放后继结点的地址
Node nextSite;
public Node(T data, Node nextSite) {
this.data = data;
this.nextSite = nextSite;
}
}
public SinglyList() {
//初始化头结点head
this.head = new Node(null, null);
//初始化链表长度L
this.L = 0;
}
//创建获取链表长度方法length()
public int length() {
return L;
}
//创建清空链表方法clear()
public void clear() {
head.nextSite = null;
this.L = 0;
}
//判断链表是否为空,构造isEmpty()方法
public boolean isEmpty() {
return L == 0;
}
//构造get()方法,获取指定位置i