public class Singlelinkedlist<AnyType> { class Node<AnyType> { public AnyType data; public Node<AnyType> next; public Node(AnyType data,Node<AnyType> next){ this.data=data; this.next=next; } } //首元节点 private Node<AnyType> first; //指向链表中第一个元素所在的结点 //头指针 private Node<AnyType> head; //指向头节点 //链表长度 int thesize; //初始化链表 public boolean initlist(){ thesize=0; first=new Node<>(null,null); head=new Node<>(null,first); return true; } //判断链表是否为空 public boolean isEmpty(){ return thesize==0; } //获取节点 public Node<AnyType> getNode(int i){ Node<AnyType> renode=head; for(int j=0;j<i;j++){ renode=renode.next; } return renode; } //在头部添加元素 public void addFirst(AnyType a){ Node<AnyType> renode=new Node<>(a,null); renode.next = first; first = renode; head.next = first; thesize++; } //打印链表 public void print(){ for(int i=1;i<thesize;i++){ System.out.print(getNode(i).data+"--"); } System.out.println(getNode(thesize).data); } public static void main(String[] args) { Singlelinkedlist<Integer> l = new Singlelinkedlist(); l.initlist(); for(int i =0;i<3;i++){ l.addFirst(i+1); } l.print(); // Outter.Inner inner = outter.new Inner();Singlelinkedlist<Integer>.Node<Integer> fir= l.new Node<>(null,null);//调用内部类,必须铜鼓Singlelinkedlist对象来实现 fir = l.first; System.out.println(fir.data); }
}
3--2--1
3
调用内部类还有其他方法 https://baijiahao.baidu.com/s?id=1705524150855334181&wfr=spider&for=pc