publicclassLinkList{ privateNode head; privateNode current; privatevoid add(int data){ if(head ==null){ head =newNode(data,null); current = head; }else{ //创建新的结点 Node node =newNode(data,null); //新创建的节点和列表进行关联 current.next = node; //移动当前链表的索引位置 current = node; } } privatevoid print(Node node){ if(node ==null){ return; } Node current = node; while(current !=null){ System.out.println(current.data); current = current.next; } } /** * @ClassName: Node * @Description: TODO(定义Node数据类型) */ classNode{ int data; Node next; publicNode(){ } publicNode(int data,Node next){ super(); this.data = data; this.next = next; } } publicstaticvoid main(String[] args){ LinkList list =newLinkList(); for(int i =0; i <10; i++){ list.add(i); } list.print(list.head); } }
原文:http://www.cnblogs.com/androidsuperman/p/4454862.html