package LinkList;
public 
class Node {
      public Object data;
      public Node next;
     public Node(){
    	 this.data=null;
    	 this.next=null;
     }
     
}
package LinkList;
public class LinkList {
     public Node head;
     public int length;
     
     public LinkList(){
    	 head=new Node();  //初始化头节点,否则会报空指针
异常
    	 length=0;
     }
     
     public void headAdd(Object obj){ //从头部插入节点
          Node node =new Node();   //新建一个节点,存放待插入的数据
          node.data=obj;
          node.next=head.next;
          head.next=node;
          length++;
    	 
     }
     
     public void tailAdd(Object obj){  //从末尾插入节点
    	// Node current=head.next;  //如果用的是这行,那么在链表里没有节点时插入会有空指针异常。
    	 Node current=head;
    	 while(current.next!=null){   //找到末尾的节点
    		 current=current.next;
    	 }
    	 
    	 Node node=new Node();
    	 node.data=obj;
    	 node.next=current.next;
    	 current.next=node;
    	 length++;
     }
     
     public void display(){  //输出链表
    	 Node current=head.next;
    	 System.out.print("the  " +length+" elements is:");
         while(current!=null){
    		 System.out.print(current.data+"  ");
    		 current=current.next;
    }
     }
     
     public void delete(Object obj){   //根据值
删除节点
    	 Node current=head;
    	 while(current.next.data!=obj){ //找到待删除节点的前一个节点
    		 current=current.next;
    	 }
    	 current.next=current.next.next;
    	 length--;
    	 
     }
     
     public Object get(int pos){
    	 if(pos<1||pos>length){  //位置不合法
    		 return false;
    	 }else{
    	 Node node=head.next;
    	 int i=1;
    	 while(i<pos){
    		 node=node.next;
    		 i++;
    	 }
    	 return node.data;
    	 }
     }
     public static void main(String[] args) {
		LinkList ll=new LinkList();
        ll.tailAdd(4);
        ll.headAdd(3);
        ll.headAdd(2);
      //  ll.delete(3);
        ll.headAdd(1);
        ll.tailAdd(5);
        for(int i=6;i<50;i++)
        {
        	ll.headAdd(i);
        	}
        ll.display();
        System.out.println();
        System.out.println("the third element is:   "+ll.get(3));
        
	}
}