【java API基本实现】LinkedList
LinkedList:
1 package com.tn.arraylist; 2 3 public class LinkedList { 4 Node head=null; 5 Node tail=null; 6 int index=0; 7 8 public void add(Node n){ 9 if(head==null){ 10 head=n; 11 tail=n; 12 }else{ 13 tail.setNext(n); 14 tail=n; 15 } 16 index++; 17 } 18 19 public int size(){ 20 return index; 21 } 22 }
Node:
1 package com.tn.arraylist; 2 3 public class Node { 4 Object data; 5 Node next; 6 public Node(Object data) { 7 super(); 8 this.data = data; 9 } 10 public void setNext(Node next) { 11 this.next = next; 12 } 13 }
客户端测试类Test:
1 package com.tn.arraylist; 2 3 public class Test { 4 public static void main(String[] args) { 5 LinkedList list=new LinkedList(); 6 for(int i=0;i<99;i++){ 7 list.add(new Node(1)); 8 } 9 System.out.println(list.size()); 10 } 11 }