链表
 
链表分为: 单链表,双链表, 循环链表
 
下面是:对单链表的回忆:
 
  看看链表的存储模型:线性的
 
添加过程:
删除过程:
下面具体给点代码:
package 线性.单链表;

public class Node{
    
     private Object value;

     private Node next;

  public Object getValue() {
    return value;
  }
    
  public void setValue(Object value) {
    this.value = value;
  }
    
  public Node getNext() {
    return next;
  }
    
  public void setNext(Node next) {
    this.next = next;
  }
    
  public Node(Object value, Node next) {
      this.value = value;
      this.next = next;
  }
  public Node(Object value) {
    this.value = value;
    this.next = null;
  }
  public Node() {
    this.value = null;
    this.next = null;
  }
}


package 线性.单链表;
public class DireLinkedList {
    /**
     * true: 是有头结点的链表
     * flase: 无头结点的链表
     */

    private boolean isHead;
    private Node first;
    /**
     * 无头节点的单链表
     */

    public DireLinkedList(Node first) {
    this.first = first;
    isHead=(first==null)?true:false;
}
    /**
     * 有头节点的单链表
     */

    public DireLinkedList() {
     first=new Node();
     isHead=true;
    }
    public DireLinkedList(DireLinkedList dll) {
    this.isHead=dll.isHead;
    this.first=dll.first;
}
    /**
     * 在index后面添加一个新的节点到链表中
     * 返回是否添加成功
     * @return
     */

    public void add(Object value,int index){
            if(index==1){
             this.addFirst(value);
            }else{
             Node node = this.get(index-1);
             Node nextNode = node.getNext();
             node.setNext(new Node(value,nextNode));
            }
    }
    /**
     * 在最后插入
     * @param index
     * @return
     */

    public void addLast(Object value){
     Node node=first;//存放当前最后节点
     while(node.getNext()!=null){
        node=node.getNext();        
     }
     node.setNext(new Node(value));
    }
    /**
     * 在第一个位置插入
     * @param index
     *
     */

    public void addFirst(Object value){
            if(isHead){//如果有头节点
             first.setNext(new Node(value,first.getNext()));
            }else{
             first=new Node(new Node(value,first));
            }    
    }

    /**
     * 是否包含 值为value的节点
     * @param value
     * @return
     */

    public boolean contain(Object value){
     Node temp;//获取第一个元素
     temp=isHead?first.getNext():first;
     while(temp!=null){
        if(temp.getValue().equals(value)) return true;
        temp=temp.getNext();
     }
     return false;
    }
    /**
     * 返回 所有的值
     * @return
     */

    public Object[] getValues(){
     Object []results=new Object[size()];
     int index=0;
     Node temp;
     temp=isHead?first.getNext():first;
     while(temp!=null){
        results[index++]=temp.getValue();
        temp=temp.getNext();
     }
     return results;
    }
    /**
     * 获取指定下面元素
     * @param index 索引
     * @return    
     */

    public Node get(int index){
     try{
        if(index<=0||index>size()){
         throw new Exception("the index out of the rang of size!!");
        }else{
         int i=0;
         Node temp;//获取第一个元素
         temp=isHead?first.getNext():first;
         while(temp!=null){
            if(++i==index) return temp;
            temp=temp.getNext();
         }
        }
     }catch(Exception e){
        e.printStackTrace();
     }
     return null;
    }
    /**
     * 获取指定下面元素
     * @param index 索引
     * @return    
     */

    public Object getValue(int index){
     try{
         if(index<=0||index>size()){
         throw new Exception("the index out of the rang of size!!");
        }else{
         int i=0;
         Node temp;//获取第一个元素
         temp=isHead?first.getNext():first;
         while(temp!=null){
            if(++i==index) return temp.getValue();
            temp=temp.getNext();
         }
        }
     }catch(Exception e){
        e.printStackTrace();
     }
     return null;
    }
    /**
     * 返回链表长度
     */

    public int size(){
     Node node=first;
     int size=1;
     while(node.getNext()!=null) {
        size++;
        node=node.getNext();
     }
     if(isHead) size--;
     return size;
    }
    /**
     * 如果存在 并且 移除返回true
     * 如果不存在 或者 没有不存在返回false
     * @param value
     * @return
     */

    public boolean removeFirst(Object value){
     if(this.contain(value)){
        Node temp;
        temp=isHead?first.getNext():first;
        Node pre=temp;
        while(temp!=null){                
         if(temp.getValue().equals(value)){
            /**
             * 移除
             */

         if(temp==pre){ //说明是第一个    
            this.remove(1);
         }else{
            pre.setNext(temp.getNext());
         }
            pre=null;
            break;
            }else{
             pre=temp;
            }
         temp=temp.getNext();
        }
        return true;
     }else    return false;
    }
    /**
     * 如果存在 并且 移除返回true
     * 如果不存在 或者 没有不存在返回false
     * @param value
     * @return
     */

    public boolean removeAll(Object value){

     if(this.contain(value)){
        Node temp;
        temp=isHead?first.getNext():first;
        Node pre=temp;
        while(temp!=null){                
         if(temp.getValue().equals(value)){

         if(temp==pre){ //说明是第一个    
            this.remove(1);
         }else{
            pre.setNext(temp.getNext());    
         }
         }else{
             pre=temp;
         }
         temp=temp.getNext();
        }
        pre=null;
        return true;
     }else    return false;
    }
    /**
     * 删除第i个节点
     */

    @SuppressWarnings("uncheck")
    public boolean remove(int i){
     Node node ;
     try{
         if(i<=0||i>size()){
            throw new Exception("the index out of the rang of size!!");
         }else if(i==1){
                 if(isHead){
                                            node=first.getNext();
                                            first.setNext(node.getNext());
                                            node=null;
                 }else{
                    node=first;
                    first.setNext(node.getNext());
                    node=null;
                    }
         }else{
            Node node1=this.get(i-1);
            Node node2=this.get(i+1);
            node1.setNext(node2);
            Node currentNode=this.get(i);
            currentNode=null;//释放当前节点
         }
     }catch(Exception e){
        e.printStackTrace();
     }
     return false;
    }
    /**
     * 替换掉第I个节点
     */

    public void replace(int i,Object value){
     Node node=this.get(i);
     node.setValue(value);
    }
    /**
     * 替换掉第I个节点
     */

    public void replace(Object current,Object value){
     Node temp=isHead?first.getNext():first;
     while(temp!=null){    
         if(temp.getValue().equals(current)){
            temp.setValue(value);
            break;
         }
                temp=temp.getNext();        
     }
    }
    /**
     *    
     * @param current 当前值
     * @param value        替换后的值
     * @return 如果一个也没有找到返回false 否则返回true
     */

    public void replaceAll(Object current,Object value){
     Node temp=isHead?first.getNext():first;
     while(temp!=null){    
         if(temp.getValue().equals(current)){
            temp.setValue(value);
         }
                temp=temp.getNext();        
     }
    }

  public boolean isHead() {
    return isHead;
  }
  public void setHead(boolean isHead) {
    this.isHead = isHead;
  }
  public Node getFirst() {
    return first;
  }
  public void setFirst(Node first) {
    this.first = first;
  }

}